Home > Web Front-end > JS Tutorial > body text

Practical methods to prevent repeated clicks and too fast clicks in javascript

PHPz
Release: 2018-10-10 15:31:39
forward
2688 people have browsed it

To prevent repeated clicks, you can add a switch, so that the switch defaults to true, and the first click changes it to false. The execution of the click event needs to determine whether the switch is true. If it is true, it will be executed, and if it is false, it will not be executed. An example is as follows:

var isclick= true;
function click(){
    if(isclick){
       isclick = false;
       //下面添加需要执行的事件
        ...
    }
}
Copy after login

Of course, if you just want to prevent clicking too fast, you can also set a timer to automatically change the switch to true after a certain period of time. The following example shows that after 500 milliseconds, the switch automatically changes to true. is true.

var isclick= true;
function click(){
    if(isclick){
        isclick= false;
        //下面添加需要执行的事件
            ...

        //定时器
        setTimeout(function(){ 
            isclick = true;
        }, 500);
    }
}
Copy after login

For more related tutorials, please visit JavaScript Video Tutorial

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template