web page timer settings

window.setInterval()
function: call a function or evaluate an expression according to the specified period (in milliseconds).
Syntax: setInterval(code, millisec)

Explanation: code: The JavaScript code string to be executed when the timer expires.
millisec: The set timing, expressed in milliseconds.
Return value: The ID value of the timer, which can be used by the clearInterval() method to stop the specified timer.
Note: The setInterval() method will keep calling the function until the timer is terminated with clearInterval() or the window is closed.

window.clearInterval()
function: Cancel the timer set by the setInterval() method.
Syntax: clearInterval(id_of_setinterval)
Explanation: id_of_setinterval: The ID value returned by setInterval(). This value identifies a setInterval timer.
That is: window.setInterval() returns the parameters of window.clearInterval

Example :

<script type="text/javascript">
var count = 0;
var timeID;
function timeCount(){
  document.getElementByIdx('timetxt').value = count;
  count++;
}
function beginCount(){
  timeID = setInterval("timeCount()",1000);
}
function stopCount(){
  clearInterval(timeID);
}
</script>
<input type="button" value="开始计时" onclick="beginCount()" />
<input type="text" id="timetxt" size="5" />
<input type="button" value="停止计时" onclick="stopCount()" />

Another example:
var objTimer = window.setInterval("moveDiv()",10) is to mobilize the timer, where moveDiv is a function of js
if(objTimer) window.clearInterval(objTimer) is to stop the timer, which is

often used after a button click Countdown, such as button to send verification code, etc. .

//countdown
var InterValObj; //timer variable, control time
var count = 60; //Interval function, executed in 1 second
var curCount;//Current remaining seconds
function countSecond() {
    curCount = count;
    $("#validate_btn").attr("disabled", "true");
    InterValObj = window.setInterval(SetRemainTime, 1000); //Start the timer and execute it once in 1 second
}

function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//Stop the timer
        $("#validate_btn").removeAttr("disabled");//Enable button
        $("#validate_btn").val("Resend verification code");
    }
    else {
        curCount--;
        $("#validate_btn").val("Send again to wait" + curCount + "seconds");
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326447720&siteId=291194637