Home >Web Front-end >JS Tutorial >How to set picture time in javascript

How to set picture time in javascript

coldplay.xixi
coldplay.xixiOriginal
2021-04-12 17:10:312962browse

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】.

How to set picture time in javascript

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 &#39;0&#39;+n;
         }else{
             return &#39;&#39;+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(&#39;img&#39;);
        for(var i = 0;i<img.length;i++){
            img[i].src = &#39;img/&#39;+sr[i]+&#39;.png&#39;;
        }
    }
    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn