Home  >  Article  >  Web Front-end  >  How to set the time interval between two consecutive button clicks in javascript_javascript skills

How to set the time interval between two consecutive button clicks in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:32:552911browse

The example in this article describes how to set the time interval between two consecutive clicks of a button using javascript. I would like to share it with you for your reference. The specific implementation method is as follows:

Many times in actual applications, we may not want the button to be clicked continuously, so we need to limit a certain time interval before the button can be clicked again. The following is a code example to introduce how to implement this function. The code is as follows :

Copy code The code is as follows:




http://www.jb51.net/" />
Script Home



0



The above code realizes our requirements and can limit the interval between clicking buttons. This effect can be extended to other functions, such as limiting the interval between posts, etc. The following is an introduction to its implementation process.

The code comments are as follows:

1.window.onload=function(){}, which stipulates that the code in the function is executed after the document content is completely loaded.
2.var odiv=document.getElementById("thediv"), get the div element object.
3.var obt=document.getElementById("bt"), get the button object.
4.var count=0, declare a variable and assign the initial value to 0, which is used to store the interval time.
5.var flag=null, declare a variable and assign the initial value to null. This variable is used to store the return value of the timer function.
6. function done(){}, this function can be called continuously by the timer function to decrement the count.
7.if(count==0){clearInterval(flag);}, if count==0, stop the execution of the timer function.
8.else{count=count-1;}, if it is not equal to 0, perform a subtraction operation.
9.obt.onclick=function(){}, register the click event processing function for the button.
10.var val=parseInt(odiv.innerHTML), get the content in the div and convert it to an integer.
11.if(count==0){
odiv.innerHTML=val 1;
count=20;
flag=setInterval(done,1000);
}
If the count is equal to 0, then add the content in the div to 1, set the count to 20, and start the execution of the timer function at the same time.
12.else{alert("It still takes" (count) "seconds to click");}, if count is not equal to zero, then how long will it take before the pop-up can be clicked.

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn