在 underscore.js 的油门实现中,'remaining > wait' 条件语句何时为真?
When is the 'remaining > wait' conditional statement ever true in underscore.js's implementation of throttle?
图书馆代码(有问题的第860行):
https://github.com/jashkenas/underscore/blob/master/underscore.js
if (remaining <= 0 || remaining > wait)
后半部分什么时候是真的?
背景 - 首先是 post SO 并且是 javascript 编码的新手。作为练习,我从头开始重新实现了 throttle,并将我的版本与库函数进行比较。我不明白为什么这部分条件语句存在于库函数中,因为在我看来它永远不会为真,所以我想我遗漏了一些东西。有人可以提供所引用陈述为真的情况来补充我吗?
我已经通过调试器 运行 它并在谷歌上搜索了文章,但没有找到答案。
全库函数:
_.throttle = function(func, wait, options) {
var timeout, context, args, result;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
var throttled = function() {
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) { // THIS LINE
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
throttled.cancel = function() {
clearTimeout(timeout);
previous = 0;
timeout = context = args = null;
};
return throttled;
};
我无法想象 'remaining' 会大于 'wait.' 这什么时候会发生?
此条件处理在 throttle
运行期间更改系统时间的情况。
这看起来像是一个超级边缘案例,但实际上有很多原因可以改变时间。您可以手动更改系统时间,也可以自动更改(例如,由于与 ntp server), you can travel and your timezone is changed and of course, don't forget about DST.
同步
我制作了一个 playground 供您深入研究。
此提交中引入了此条件:Allow _.throttle to function correctly after the system time is updated。
P.S。我几乎在每个项目中都面临与时间相关的问题,我非常感谢所有考虑这些事情的人。
图书馆代码(有问题的第860行): https://github.com/jashkenas/underscore/blob/master/underscore.js
if (remaining <= 0 || remaining > wait)
后半部分什么时候是真的?
背景 - 首先是 post SO 并且是 javascript 编码的新手。作为练习,我从头开始重新实现了 throttle,并将我的版本与库函数进行比较。我不明白为什么这部分条件语句存在于库函数中,因为在我看来它永远不会为真,所以我想我遗漏了一些东西。有人可以提供所引用陈述为真的情况来补充我吗?
我已经通过调试器 运行 它并在谷歌上搜索了文章,但没有找到答案。
全库函数:
_.throttle = function(func, wait, options) {
var timeout, context, args, result;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
var throttled = function() {
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) { // THIS LINE
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
throttled.cancel = function() {
clearTimeout(timeout);
previous = 0;
timeout = context = args = null;
};
return throttled;
};
我无法想象 'remaining' 会大于 'wait.' 这什么时候会发生?
此条件处理在 throttle
运行期间更改系统时间的情况。
这看起来像是一个超级边缘案例,但实际上有很多原因可以改变时间。您可以手动更改系统时间,也可以自动更改(例如,由于与 ntp server), you can travel and your timezone is changed and of course, don't forget about DST.
同步我制作了一个 playground 供您深入研究。
此提交中引入了此条件:Allow _.throttle to function correctly after the system time is updated。
P.S。我几乎在每个项目中都面临与时间相关的问题,我非常感谢所有考虑这些事情的人。