Home >Web Front-end >JS Tutorial >How to set picture time in javascript
Javascript method to set picture time: get the current time and convert it to a string, loop to get the string and change the node picture path, the code is [var sr = toDou(time.getHours()) toDou(time .getMinutes()) toDou】.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
Javascript method of setting picture time:
<script type="text/javascript"> function toDou(n){//将十以内的数字转换成两位 if(n<10){ return '0'+n; }else{ return ''+n; } }
Get the current time and convert it to a string, loop to obtain the string and change the node picture path
function tick(){ var time = new Date(); var sr = toDou(time.getHours())+toDou(time.getMinutes())+toDou(time.getSeconds()); var img = document.getElementsByTagName('img'); for(var i = 0;i<img.length;i++){ img[i].src = 'img/'+sr[i]+'.png'; } } window.οnlοad=function(){ tick(); setInterval(tick, 1000); } <script>
Related free learning recommendations: javascript video tutorial
The above is the detailed content of How to set picture time in javascript. For more information, please follow other related articles on the PHP Chinese website!