how to update time regularly?

后端 未结 10 867
再見小時候
再見小時候 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: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);
    

提交回复
热议问题