Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重置设置超时_Javascript_Jquery - Fatal编程技术网

Javascript 重置设置超时

Javascript 重置设置超时,javascript,jquery,Javascript,Jquery,我有以下资料: window.setTimeout(function() { window.location.href = 'file.php'; }, 115000); 如何通过.click函数在倒计时中途重置计数器?clearTimeout()并输入setTimeout的引用,该引用将是一个数字。然后重新调用它: var initial; function invocation() { alert('invoked') initial = window.setTi

我有以下资料:

window.setTimeout(function() {
    window.location.href = 'file.php';
}, 115000);
如何通过.click函数在倒计时中途重置计数器?

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秒内未单击body元素,则背景颜色将为黑色

参考:

注意:setTimeout和clearTimeout不是ECMAScript本机方法,而是全局窗口命名空间的Javascript方法

var myTimer = setTimeout(..., 115000);
something.click(function () {
    clearTimeout(myTimer);
    myTimer = setTimeout(..., 115000);
}); 

沿着这些路线的东西

您可以存储对该超时的引用,然后调用该引用

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

您必须记住超时“计时器”,取消它,然后重新启动它:

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();
}
此计时器将在30秒后触发“Hello”警报框。但是,每次单击“重置计时器”按钮时,它都会清除计时器指针,然后重新设置。一旦开火,游戏就结束了

<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 timerHandle=setTimeout(“警报('Hello')”,3000);
函数resetTimer(){
clearTimeout(timerHandle);
timerHandle=setTimeout(“警报('Hello')”,3000);
}
重置计时器

要重置计时器,需要设置并清除计时器变量

$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
});
postimer('string',int,function(){
你的代码在这里
});

对于NodeJS它非常简单:

const timeout = setTimeout(...);

timeout.refresh();
从文档中:

超时。刷新()

将计时器的开始时间设置为当前时间,并重新安排计时器以在调整为当前时间的先前指定的持续时间调用其回调。这对于在不分配新JavaScript对象的情况下刷新计时器非常有用


但是它在JavaScript中不起作用,因为在浏览器中,
setTimeout()
返回一个数字,而不是一个对象。

Argh,我建议不要在JavaScript中使用php风格的变量名。是的,它会工作,但是我的天,它让人困惑。奇怪的是,超时对象本身没有一个
.clear()
。@Cort3z这是因为
window.setTimeout
返回一个数字(计时器的ID)而不是一个“timeout对象”。是@DanO:奇怪的是,setTimeout没有返回超时对象。在NodeJS上下文中确实如此。有趣的是,此选项已被忽略。其他的似乎都需要复制计时器内部函数,如果要维护两次以上的代码行,那么这就不那么枯燥和痛苦了……您可能可以使用现有的去盎司实现。
afterTimer('<timername>string', <interval in milliseconds>int, function(){
   your code here
});
const timeout = setTimeout(...);

timeout.refresh();