2016-11-21 164 views
-3

我在我的網站上有這個計時器,我使用下面的腳本來改變日期和時間。如何重置Javascript上的計時器?

我想進行一些編輯,以便每天早上12點自動重置。

另外,我希望腳本檢查客戶端的時間並根據用戶的時間進行重置。

有人可以幫忙嗎?

<script> 
// set the date we're counting down to 
var target_date = new Date("Nov 23, 2016 0:00:00").getTime(); 

// variables for time units 
var days, hours, minutes, seconds; 

// get tag element 
var countdown = document.getElementById("countdown"); 

// update the tag with id "countdown" every 1 second 
setInterval(function() { 

// find the amount of "seconds" between now and target 
var current_date = new Date().getTime(); 
var seconds_left = (target_date - current_date)/1000; 

// do some time calculations 
days = parseInt(seconds_left/86400); 
seconds_left = seconds_left % 86400; 

hours = parseInt(seconds_left/3600); 
seconds_left = seconds_left % 3600; 

minutes = parseInt(seconds_left/60); 
seconds = parseInt(seconds_left % 60); 

// format countdown string + set tag value 
// countdown.innerHTML = days + "d, " + hours + "h, " 
// + minutes + "m, " + seconds + "s"; 
$('.days').html(days) 
$('.hours').html(hours) 
$('.minutes').html(minutes) 
$('.seconds').html(seconds) 


}, 1000); 
</script> 
+0

[在JavaScript停止的setInterval電話]的可能的複製(http://stackoverflow.com/questions/109086/stop-setinterval-call-in-javascript) –

回答

1

您需要將您的setInterval分配給一個變量,然後您可以使用在致電clearInterval,如下所示:

var timer = setInterval(.... 

並予以清除:

clearInterval(timer);