2
讓我知道設置clearTimeout和將相同變量設置爲null用於設置settimeout的id之間的區別。設置clearTimeOut
我在下面提到的underscore.js中看到了一個函數。
function debounce(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = new Date().getTime() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = new Date().getTime();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
它最終將「timeout」變量設置爲「null」而不是使用clearTimeOut。
感謝您接受我的回答,這不是很清楚,所以如果任何社區成員想要編輯它,請隨時取消。 – PaulBGD