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

How to prevent the same event from being triggered repeatedly

php中世界最好的语言
Release: 2018-03-14 17:50:31
Original
2187 people have browsed it

This time I will bring you how to prevent the same event from being triggered repeatedly, and what are the precautions to prevent the same event from being triggered repeatedly. The following is a practical case, let’s take a look one time.

Repeated triggering is to prevent users from repeatedly clicking to submit data. We usually click again if there is no response after clicking. This must not only be done from the user experience point of view, but also needs to be done in js or php program scripts. Okay, let the user know that the click is submitted and the server is processing it. Now I will sort out the script to deal with this repeated triggering problem.

Many times events will be triggered repeatedly quickly, such as click, which will execute the code twice, causing many consequences. There are many solutions now, but almost all have limitations. For example, in an Ajax form, if the user is prevented from clicking multiple times at once, the submit button can be frozen when the user clicks it for the first time, and then released when another click is allowed. Many people do this, but it doesn't work very well in other situations.

The following is a good method recommended. First, throw a function into it.

var _timer = {}; 
function delay_till_last(id, fn, wait) { 
 if (_timer[id]) { 
  window.clearTimeout(_timer[id]); 
  delete _timer[id]; 
 } 
 return _timer[id] = window.setTimeout(function() { 
  fn(); 
  delete _timer[id]; 
 }, wait); 
}
Copy after login

Usage method

$dom.on('click', function() { 
 delay_till_last('id', function() {//注意 id 是唯一的 
  //响应事件 
 }, 300); 
});
Copy after login

The above code can wait for 300 milliseconds after clicking. If it is within 300 milliseconds If this event occurs again, the last click will be cancelled, and the timer will be restarted. Repeat this process until it has waited for 300 milliseconds before responding to the event.

This function is very useful, such as verifying input or pulling the avatar in real time based on the entered email address without having to wait until it is out of focus before pulling it.

Example

Button BUTTON class

a Label class

For the first type of situation, the button has an attribute that is disabled to control whether it can be clicked. See the code

<input type="button" value="Click" id="subBtn"/> 
<script type="text/javascript"> 
function myFunc(){ 
 //code 
 //执行某段代码后可选择移除disabled属性,让button可以再次被点击 
 $("#subBtn").removeAttr("disabled"); 
} 
$("#subBtn").click(function(){ 
 //让button无法再次点击 
 $(this).attr("disabled","disabled"); 
 //执行其它代码,比如提交事件等 
 myFunc(); 
}); 
</script>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese Other related articles online!

Recommended reading:

How to achieve the image carousel effect

##The jQuery method to implement a timed hiding dialog box

The above is the detailed content of How to prevent the same event from being triggered repeatedly. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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