how to update time regularly?

后端 未结 10 849
再見小時候
再見小時候 2020-12-03 02:11
function timeClock()
{
    setTimeout(\"timeClock()\", 1000);        
    now = new Date();
    alert(now);
    f_date = now.getDate()+\" \"+strMonth(now.getMonth())         


        
相关标签:
10条回答
  • 2020-12-03 02:19

    I'd use setInterval rather than setTimeout:

    https://developer.mozilla.org/en/DOM/window.setInterval

    0 讨论(0)
  • 2020-12-03 02:24

    I used @soloproper setInterval concept @Ivo Wetzel overall answer, my update is all about formatting the time as required. Reduced Programming Lines

    <div id="liveClock"> </div>
    
    $(document).ready(function () {
            updateClock();
    });
    
    function updateClock() {
       document.getElementById('liveClock').innerHTML = new Date().format("dd/MMMM/yyyy hh:mm:ss tt");
    }
    setInterval(updateClock, 1000);
    
    0 讨论(0)
  • 2020-12-03 02:26
    x = document.getElementsByTagName('SPAN').item(0);
    x.innerHTML = f_date;
    

    try putting this code block instead of return statement, i haven't test it but it will probably work

    0 讨论(0)
  • 2020-12-03 02:27
    function timeClock()
    {
        setTimeout("timeClock()", 1000);        
        now = new Date();
        alert(now);
        f_date = now.getDate()+" "+strMonth(now.getMonth())+" "+now.getFullYear()+" / "+timeFormat(now.getHours(), now.getMinutes());
    
        document.getElementById("foo").innerHTML = f_date;
    
    }
    
    <span id="foo"></span>
    
    0 讨论(0)
  • 2020-12-03 02:32

    Straigt Javascript time format / update

    1: create month converter func 2: create time func 3: create update func 4: create outPut func

        // month converter from index / 0-11 values
    function covertMonth(num){
      let months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
      // look into index with num 0-11
      let computedRes = months[num];
      return computedRes;
    }
    
    // time func
    function Time(){
       // important to get new instant of the Date referrance
      let date = new Date();
      this.time = date.toLocaleTimeString();
      this.year = date.getUTCFullYear();
      this.day = date.getUTCDate();
      this.month = date.getUTCMonth();
      this.currentTime = date.toLocaleTimeString() + ' ' + covertMonth(this.month) + ' ' + this.day + ' ' + this.year;
      return this.currentTime;
    }
    
    
     function timeOutPut(){
      let where = document.getElementById('some-id');
      where.textContent = Time(); // 1:21:39 AM Dec 17 2017
    }
    
       // run every 5secs
    setInterval(timeOutPut, 5000);
    
    0 讨论(0)
  • 2020-12-03 02:33
    $('span.foo').html(f_date);
    

    place this inside your timeclock() function

    untested

        function timeClock()
        {
            setTimeout("timeClock()", 1000);        
            now = new Date();
            alert(now);
            f_date = now.getDate()+" "+strMonth(now.getMonth())+" "+now.getFullYear()+" / "+timeFormat(now.getHours(), now.getMinutes());
           $('span.foo').html(f_date);
    
    }
    
    0 讨论(0)
提交回复
热议问题