admin管理员组文章数量:1319007
_.debounce()
fires at most evevry x milliseconds with _.debounce(function,x
) .. I want to adapt this to only execute a method x
millis after the last _.debounce()
.
How do I go about this? (I've read that $.debounce
does exactly that btw.)
I've tried to do this, but it isn't bullet-proof (not to mention butt-ugly)
var timeout;
$(window).on("resize",_.debounce(function(){
if(timeout){
clearTimeout(timeout);
}
//when debounce es in we cancel it.. this means only the latest debounce actually fires.
//not bullet proof
timeout = setTimeout(resizeMap,100);
},50));
How to do this elegantly?
_.debounce()
fires at most evevry x milliseconds with _.debounce(function,x
) .. I want to adapt this to only execute a method x
millis after the last _.debounce()
.
How do I go about this? (I've read that $.debounce
does exactly that btw.)
I've tried to do this, but it isn't bullet-proof (not to mention butt-ugly)
var timeout;
$(window).on("resize",_.debounce(function(){
if(timeout){
clearTimeout(timeout);
}
//when debounce es in we cancel it.. this means only the latest debounce actually fires.
//not bullet proof
timeout = setTimeout(resizeMap,100);
},50));
How to do this elegantly?
Share Improve this question asked Dec 5, 2012 at 18:09 Geert-JanGeert-Jan 18.9k19 gold badges81 silver badges145 bronze badges 2-
1
It's kind of confusing what you're after. Can you explain a bit more? Do you want to debounce your debounce? Yo dawg... What you describe seems to be exactly what
debounce
is designed for. Why do you need an additional timeout? – Alex Wayne Commented Dec 5, 2012 at 18:13 - hehe.. well perhaps it's my browser (infrequent resize events, causing _debounce to be called? testing on Chrome), but while resizing, I keep getting multiple calls to the body of the debounced function. As if it's behaving exactly as _.throttle now I e to think of it.. Weird stuff. – Geert-Jan Commented Dec 5, 2012 at 19:01
2 Answers
Reset to default 4After reading your ment, this is clearer now.
well perhaps it's my browser (infrequent resize events, causing _debounce to be called? testing on Chrome), but while resizing, I keep getting multiple calls to the body of the debounced function. As if it's behaving exactly as _.throttle now I e to think of it.. Weird stuff.
50ms is a pretty low debounce time. I'm betting it was working as intended, and you just need a longer debounce time. 50ms is 1/20th of a second. I'm not sure the window resize event fires that quickly. But even if it does, the tiniest pause in mouse movement while resizing could triggers this.
Remove all this setTimeout
nonsense in your debounced function and set the debounce time to something more like 250
and I bet it will work just like you want.
From http://underscorejs/#debounce:
_.debounce(function, wait, [immediate])
Pass
true
for theimmediate
parameter to causedebounce
to trigger the function on the leading instead of the trailing edge of the wait interval. Useful in circumstances like preventing accidental double-clicks on a "submit" button from firing a second time.
So, $(window).on("resize",_.debounce(resizeMap,100))
should just work.
本文标签: javascriptUnderscore debounce()How to execute method only the last time receivedStack Overflow
版权声明:本文标题:javascript - Underscore _.debounce() : How to execute method only the last time received? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742053199a2418158.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论