如何定期更新时间?

function timeClock() { setTimeout("timeClock()", 1000); now = new Date(); alert(now); f_date = now.getDate()+" "+strMonth(now.getMonth())+" "+now.getFullYear()+" / "+timeFormat(now.getHours(), now.getMinutes()); return f_date; } document.write(timeClock()); 

警报(现在的); 每秒给我一个值,但它没有在html中更新。 如何在不刷新页面的情况下更新html的时间?

您的代码中存在许多错误。 如果不使用变量声明的var infront,则将它们泄漏到全局范围中。

另外, 不鼓励使用document.write

这是我将如何做到这一点:

JavaScript的:

 function updateClock() { var now = new Date(), // current date months = ['January', 'February', '...']; // you get the idea time = now.getHours() + ':' + now.getMinutes(), // again, you get the idea // a cleaner way than string concatenation date = [now.getDate(), months[now.getMonth()], now.getFullYear()].join(' '); // set the content of the element with the ID time to the formatted string document.getElementById('time').innerHTML = [date, time].join(' / '); // call this function again in 1000ms setTimeout(updateClock, 1000); } updateClock(); // initial call 

HTML:

 

setInterval(expression,timeout);

函数setTimeout用于单个超时,因此使用setInterval将是更合适的选项。 SetInterval将定期运行,而没有Ivo的答案所具有的额外行。

我会重写Ivo的答案如下:

JavaScript的:

 function updateClock() { // Ivo's content to create the date. document.getElementById('time').innerHTML = [date, time].join(' / ') } setInterval(updateClock, 1000); 

亲自尝试一下! https://jsfiddle.net/avotre/rtuna4x7/2/

 x = document.getElementsByTagName('SPAN').item(0); x.innerHTML = f_date; 

尝试把这个代码块而不是return语句,我没有测试它,但它可能会工作

在timeago jQuery插件中可能有一些东西你可以挂钩,但我还没有诚实地试过……

http://timeago.yarp.com/

 $('span.foo').html(f_date); 

将它放在你的timeclock()函数中

未经测试

  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); } 

我使用setInterval而不是setTimeout:

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

我认为你的setTmeout函数有错误的变量,第一个应该是一个函数而不是一个字符串,这让我有点困惑。 基本上,当您运行该函数时,您需要写入span标记。

我在小提琴中创建了一个jQuery版本来演示我的意思。 没有你的strMonthfunction,但你明白了。 我还将警报更改为console.log,但您可以删除该行。

http://jsfiddle.net/5JWEV/

Straigt Javascript时间格式/更新

1:创建月转换器function2:创建时间function3:创建更新function4:创建outPutfunction

  // 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); 

我使用了@soloproper setInterval概念@Ivo Wetzel的总体答案,我的更新是关于根据需要格式化时间。 减少编程线

 
$(document).ready(function () { updateClock(); }); function updateClock() { document.getElementById('liveClock').innerHTML = new Date().format("dd/MMMM/yyyy hh:mm:ss tt"); } setInterval(updateClock, 1000);
 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; }