Home  >  Article  >  Web Front-end  >  Practical methods to prevent repeated clicks and too fast clicks in javascript

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

PHPz
PHPzforward
2016-05-16 19:14:282689browse

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;
       //下面添加需要执行的事件
        ...
    }
}

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

For more related tutorials, please visit JavaScript Video Tutorial

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete