我有以下几点:

window.setTimeout(function() {
    window.location.href = 'file.php';
}, 115000);

如何通过.click功能,在倒计时中途重置计数器?


当前回答

该计时器将在30秒后发出“Hello”警报框。然而,每次你点击重置计时器按钮,它会清除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();

这里有一个详细的例子,说明到底发生了什么http://jsfiddle.net/ppjrnd2L/

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

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

该计时器将在30秒后发出“Hello”警报框。然而,每次你点击重置计时器按钮,它会清除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>
$(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])} );

    })();

});