Home >Web Front-end >JS Tutorial >How jQuery prevents the same event from being triggered repeatedly quickly

How jQuery prevents the same event from being triggered repeatedly quickly

亚连
亚连Original
2018-06-07 11:01:512107browse

Now I will share with you a jQuery method to prevent the same event from being triggered quickly and repeatedly. It has a very good reference value and I hope it will be helpful to everyone.

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

Usage method

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

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>

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Using vue2.0.js to implement multi-level linkage selectors

Using mint-ui to implement provinces and cities Three-level linkage effect

Using vue to implement secondary route setting method

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

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