English 中文(简体)
javascript/jquery - Add debounce to a button
原标题:javascript/jquery - add debounce to a button

我想在纽伦加一字,但希望每当用户点击纽顿时就采取某些行动,但只是在用户点击后5次之后,才进行更新。 通常看来,神学直接适用于听众。 在此,我要求每点子被点击时采取一些行动,然后在一段合理的等候期之后更新。

I am not sure how to use the function in this case...

参考:

$( #myButton ).click(function() {
    // do a date calculation
    // show user changes to screen
    // wait until user has has stopped clicking the 
             // button for 5 seconds, then update file with "process" function.

});

function process(){
    // update database table
}

debounce syntax

$( input ).bind( keyup blur , $.debounce(process, 5000));
最佳回答

You could still use $.debounce like so:

// create new scope
(function() {
     // create debounced function
     var dprocess = $.debounce(process, 5000);

     // bind event handler
     $( #myButton ).click(function() {
         // do a date calculation
         // show user changes to screen
         // call the function
         dprocess();
     });
}());

不含<代码>的备选案文 (你可以永远以这种方式篡改你的法典,不 j笑):

// create new scope
(function() {
     var timer;

     // bind event handler
     $( #myButton ).click(function() {
         if(timer) {
             clearTimeout(timer);
         }
         // do a date calculation
         // show user changes to screen
         // call the function
         timer = setTimeout(process, 5000);
     });
}());
问题回答

Debounce using home/vanilla JS and jquery/underscore.js.

Example

<>JS

//Native/Vanilla JS
document.getElementById( dvClickMe ).onclick = debounce(function(){
    alert( clicked - native debounce ); 
}, 250);


function debounce(fun, mil){
    var timer; 
    return function(){
        clearTimeout(timer); 
        timer = setTimeout(function(){
            fun(); 
        }, mil); 
    };
}

//jQuery/Underscore.js
$( #dvClickMe2 ).click(_.debounce(function(){
    alert( clicked - framework debounce ); 
}, 250)); 

<><><><>>><>>>>><>>>>>>

<div id= dvClickMe >Click me fast! Native</div>
<div id= dvClickMe2 >Click me fast! jQuery + Underscore</div>
 var timer;
 $( #myButton ).click(function() {
     //Called every time #myButton is clicked         
     if(timer) clearTimeout(timer);
     timer = setTimeout(process, 5000);
 });

function process(){
  //Called 5000ms after #myButton was last clicked 
}

Using a global variable might not be the best solution if the debounce function is used to debounce multiple functions. For that we should scope the timer to the function being denounced.

function debounce(func, timeout = 2000) {
  if (func.timer) clearTimeout(func.timer);
  func.timer = setTimeout(func, timeout);
}

debounce(func1, 4000);
debounce(func2, 1000);

NOTE: this won t work with an anonymous function;





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签