English 中文(简体)
建议每分钟更新一次
原标题:Suggest a way to update time every minute

我全程申请。 i 采用以下代码,每分钟更新一次。 但是,如果浏览器保持开放,则10把浏览器变成不反应/迟缓。 建议制定一部更好的法典。

function tick()
{
    var d = new Date();
    var time = padNumber(d.getHours(),2)+ : +padNumber(d.getMinutes(),2);
    $( #WeatherBoLfTime ).html(   +time);
    t = setInterval( tick() ,60000);
}

$(document).ready(function(){
    tick();
})
最佳回答

The problem is that you re calling setInterval many times and never clearing any of them. So after a while you have lots of interval callbacks running at around the same time.

变化

t = setInterval( tick() ,60000);

纽约总部

t = setTimeout(tick,60000);

When I first started out coding JavaScript I 纽约总部ok down a Lycos web server with AJAX calls because I made the same mistake :-)

Note that since you re displaying the actual time, you should use a much shorter timer than 1 minute. If I land on your webpage at 13:42:30, the time will not be updated until ~13:43:30. To keep it in sync with the machine s time, you would probably want 纽约总部 set the timer for 1000.

问题回答

<编码>Interval(> >上限。 你们只需要一劳永逸,就会自动打上每600万ms。 <代码>准时()是你必须每再一次安装的。

As others have pointed out, you are creating new interval every time. Move it to outside your function.

function tick()
{
    var d = new Date();
    var time = padNumber(d.getHours(),2)+ : +padNumber(d.getMinutes(),2);
    $( #WeatherBoLfTime ).html( &nbsp; +time);
}

$(document).ready(function(){
    t = setInterval(tick,60000);
})

Edit: I m slow and other answers seem to be updated. So this one is useless now :)

Have look in below demo code, which will be updated each second...

<!DOCTYPE html>
<html>
<head>
	<title>j</title>
	<script type="text/javascript">

   function myFunction() 
   {
       setInterval(function(){ 
            var d = new Date(); 
            document.getElementById("dates").innerHTML = new Date(d.getTime());}, 1000);
   }
			
	</script>
</head>
<body>
	<input type="button" name="e" onclick="myFunction()">
	<p id= "dates">ok</p>
</body>
</html>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签