[英]Reset _.after counter
I looked on the internet, but I was not able to find a way of resetting the _.after counter after the code inside gets executed. 我在互联网上看了看,但是在执行内部代码之后,找不到一种重置_.after计数器的方法。 In this example, my intended functionality would mean that only every 5th click on the button would show the alert box: 在此示例中,我预期的功能意味着按钮上每单击5次,便会显示一个警报框:
var cb;
cb = _.after(4, function() {
alert(':)');
});
$('button').on('click', cb);
http://jsfiddle.net/danharper/JkcuD/ http://jsfiddle.net/danharper/JkcuD/
you can't reset or even see the counter because the counter is private: 您无法重置,甚至看不到计数器,因为计数器是私有的:
from: http://underscorejs.org/docs/underscore.html#section-87 来自: http : //underscorejs.org/docs/underscore.html#section-87
_.after = function(times, func) {
return function() {
if (--times < 1) {
return func.apply(this, arguments);
}
};
};
that said, you can see there's not much code there, so adjusting it to run once-every is easy: 也就是说,您可以看到那里没有太多代码,因此将其调整为每运行一次就很容易了:
_.onceEvery= function(times, func) {
var orig = times;
return function() {
if (--times < 1) {
times=orig;
return func.apply(this, arguments);
}
};
};
example: http://jsfiddle.net/JkcuD/78/ 示例: http : //jsfiddle.net/JkcuD/78/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.