ฉันมีดังต่อไปนี้:
window.setTimeout(function() {
window.location.href = 'file.php';
}, 115000);
ฉันจะรีเซ็ตฟังก์ชั่นตัวนับตรงกลางผ่านการนับถอยหลังได้อย่างไรผ่านฟังก์ชั่น. คลิก
ฉันมีดังต่อไปนี้:
window.setTimeout(function() {
window.location.href = 'file.php';
}, 115000);
ฉันจะรีเซ็ตฟังก์ชั่นตัวนับตรงกลางผ่านการนับถอยหลังได้อย่างไรผ่านฟังก์ชั่น. คลิก
คำตอบ:
คุณสามารถจัดเก็บการอ้างอิงไปยังการหมดเวลานั้นแล้วเรียกclearTimeout
การอ้างอิงนั้น
// in the example above, assign the result
var timeoutHandle = window.setTimeout(...);
// in your click function, call clearTimeout
window.clearTimeout(timeoutHandle);
// then call setTimeout again to reset the timer
timeoutHandle = window.setTimeout(...);
.clear()
วัตถุหมดเวลาในตัวมันเอง
window.setTimeout
ส่งกลับตัวเลข (ID ตัวจับเวลา) และไม่ใช่ "วัตถุหมดเวลา"
clearTimeout () และฟีดการอ้างอิงของ setTimeout ซึ่งจะเป็นตัวเลข จากนั้นเรียกใช้อีกครั้ง:
var initial;
function invocation() {
alert('invoked')
initial = window.setTimeout(
function() {
document.body.style.backgroundColor = 'black'
}, 5000);
}
invocation();
document.body.onclick = function() {
alert('stopped')
clearTimeout( initial )
// re-invoke invocation()
}
ในตัวอย่างนี้ถ้าคุณไม่คลิกที่องค์ประกอบร่างกายใน 5 วินาทีสีพื้นหลังจะเป็นสีดำ
อ้างอิง:
หมายเหตุ: setTimeout และ clearTimeout ไม่ใช่วิธีดั้งเดิมของ ECMAScript แต่เป็นวิธี Javascript ของเนมสเปซหน้าต่างทั่วโลก
คุณจะต้องจำการหมดเวลา "ตัวตั้งเวลา" ยกเลิกแล้วเริ่มต้นใหม่:
g_timer = null;
$(document).ready(function() {
startTimer();
});
function startTimer() {
g_timer = window.setTimeout(function() {
window.location.href = 'file.php';
}, 115000);
}
function onClick() {
clearTimeout(g_timer);
startTimer();
}
var myTimer = setTimeout(..., 115000);
something.click(function () {
clearTimeout(myTimer);
myTimer = setTimeout(..., 115000);
});
บางสิ่งบางอย่างตามสายเหล่านั้น!
ตัวจับเวลานี้จะส่งสัญญาณเตือน "Hello" หลังจาก 30 วินาที อย่างไรก็ตามทุกครั้งที่คุณคลิกปุ่มตัวจับเวลารีเซ็ตมันจะลบตัวจับเวลา TimerHandle จากนั้นตั้งค่าใหม่อีกครั้ง เกมจะจบลง
<script type="text/javascript">
var timerHandle = setTimeout("alert('Hello')",3000);
function resetTimer() {
window.clearTimeout(timerHandle);
timerHandle = setTimeout("alert('Hello')",3000);
}
</script>
<body>
<button onclick="resetTimer()">Reset Timer</button>
</body>
var redirectionDelay;
function startRedirectionDelay(){
redirectionDelay = setTimeout(redirect, 115000);
}
function resetRedirectionDelay(){
clearTimeout(redirectionDelay);
}
function redirect(){
location.href = 'file.php';
}
// in your click >> fire those
resetRedirectionDelay();
startRedirectionDelay();
$(function() {
(function(){
var pthis = this;
this.mseg = 115000;
this.href = 'file.php'
this.setTimer = function() {
return (window.setTimeout( function() {window.location.href = this.href;}, this.mseg));
};
this.timer = pthis.setTimer();
this.clear = function(ref) { clearTimeout(ref.timer); ref.setTimer(); };
$(window.document).click( function(){pthis.clear.apply(pthis, [pthis])} );
})();
});
ในการรีเซ็ตตัวจับเวลาคุณจะต้องตั้งค่าและล้างตัวแปรตัวจับเวลา
$time_out_handle = 0;
window.clearTimeout($time_out_handle);
$time_out_handle = window.setTimeout( function(){---}, 60000 );
ฉันรู้ว่านี่เป็นด้ายเก่า แต่ฉันมากับสิ่งนี้ในวันนี้
var timer = []; //creates a empty array called timer to store timer instances
var afterTimer = function(timerName, interval, callback){
window.clearTimeout(timer[timerName]); //clear the named timer if exists
timer[timerName] = window.setTimeout(function(){ //creates a new named timer
callback(); //executes your callback code after timer finished
},interval); //sets the timer timer
}
และคุณเรียกใช้
afterTimer('<timername>string', <interval in milliseconds>int, function(){
your code here
});