简体   繁体   English

使用clearInterval()后如何继续计时

[英]How to continue timer after using clearInterval( );

I have the following function: 我有以下功能:

$(document).ready(function() {
    $('#p').click(function() {
        $('#s1').hide();
        gameStart(); <-starts timer
        $('#s2').show();
    });

    $('.b4').click(function() {
        clearInterval(tI); <-stops the time
    });
    $('#r').click(function() {
        tI = setInterval("setTimer()", 1000); <-not working, should continue the timer again
    });
});

When the div #p is clicked it will start a game with a timer, this all works. 单击div #p ,它将启动一个带有计时器的游戏,这一切正常。 Now if I click the div .b4 it will stop/pauze the timer, this also works. 现在,如果我单击div .b4 ,它将停止/启动计时器,这也有效。 The problem is to start the timer again, continuing from where it was stopped. 问题是要重新启动计时器,从停止的地方继续。

I've tried tI = setInterval("setTimer()", 1000); 我已经尝试过tI = setInterval("setTimer()", 1000); when clicking the div #r but the problem with this is that is will start the timer, but it will not continue from the time it was stopped. 单击div #r ,问题是将启动计时器,但从停止时间起将不会继续。 It will continue, but from the time where it should be if it wasn't stopped/pauzed. 它会继续,但是如果没有停止/暂停,它将从应该的时间开始。

SO how can I fix this? 那我该如何解决呢? I want to pause the time (think I have this part right) and then continue it again when clicking #r 我想暂停时间(认为这部分正确),然后在单击#r时再次继续

This is how my timer function is build: 这是我的计时器函数的构建方式:

function gameStart() {
    setBlocks();
    tM = $('#a1 div.tm');
    hS = $('#a1 div.hs');
    oT = new Date();
    tI = setInterval("setTimer()", 1000);
    setHs();
    $('.ht').click(hint);
    $('.b3').click(reset);
}


//Set timer
function setTimer() {
    var n = new Date();
    tM.text(getTimeText(n.getTime() - oT.getTime()));
}

//Build timer
function getTimeText(v) {
    var vH = Math.floor(v / 3600000);
    var vM = Math.floor((v - (vH * 3600000)) / 60000);
    var vS = Math.floor((v - (vH * 3600000) - (vM * 60000)) / 1000);
    return set2Digit(vH) + ':' + set2Digit(vM) + ':' + set2Digit(vS);
}

function set2Digit(ov) {
    var os = '0' + ov;
    return os.substr(os.length - 2, 2);
}

--EDIT-- - 编辑 -

With this html I get the time to show: <div class="tm">00:00:00</div> 有了这个html,我有时间显示: <div class="tm">00:00:00</div>

--EDIT2-- Okay created a new function. --EDIT2--好的,创建了一个新功能。 It grabs the paused time, so from this point the time should continue: 它抓住了暂停的时间,因此从这一点开始,时间应该继续:

$('#r').click(function() {
        $('#s1').hide();
        tI = setInterval("continueTimer()", 1000); //NEW
        $('#s2').show();
    });

//Test
function continueTimer() {
    divObj = document.getElementById("tm");
    tM.text(divObj.innerText);
}

At the moment, the time pauses but doesn't continue when this function is fired... So how do I start the timer from this point? 此刻,此功能被触发时,时间会暂停,但不会继续...那么如何从这一点启动计时器? I think I am close 我想我很近

It is because in setTimer method you are creating a new Date object. 这是因为在setTimer方法中您正在创建一个new Date对象。 So everytime this method executes it will take the current time. 因此,每次执行此方法都将花费当前时间。 If you want to continue form where the timer was stopped instead of creating new Date object everytime, maintain the start date when the timer starts in a variable and then use that variable inside setTimer method. 如果要从停止计时器的位置继续而不是每次都创建new Date对象,请在变量中维护计时器启动时的开始日期,然后在setTimer方法中使用该变量。

Modify the below method and add a global variable. 修改下面的方法并添加一个全局变量。

var initialTime;
function setTimer() {
    tM.text(getTimeText(initialTime.getTime() - oT.getTime()));
    initialTime.setMilliseconds(1000);
}

function gameStart() {
    setBlocks();
    tM = $('#a1 div.tm');
    hS = $('#a1 div.hs');
    oT = new Date();
    initialTime = new Date();
    setTimer();
    tI = setInterval("setTimer()", 1000);
    setHs();
    $('.ht').click(hint);
    $('.b3').click(reset);
}

Can you try below, 您可以尝试以下吗?

$('#r').click(function() {
    oT = new Date(); //this will move the offset date to the resume time..
    tI = setInterval(setTimer, 1000);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM