Javascript 创建自己的油门功能并使用setTimeout进行测试
我有一个任务要写我自己的油门功能。它需要使用setTimeout通过一定数量的测试 这是我的代码:Javascript 创建自己的油门功能并使用setTimeout进行测试,javascript,timeout,throttling,Javascript,Timeout,Throttling,我有一个任务要写我自己的油门功能。它需要使用setTimeout通过一定数量的测试 这是我的代码: var throttle = function(func, delay) { var counter = 0; var calledOnce = false; setInterval(function(){ counter++; }, 1); return function() { if (counter > delay || !calledOnce) {
var throttle = function(func, delay) {
var counter = 0;
var calledOnce = false;
setInterval(function(){
counter++;
}, 1);
return function() {
if (counter > delay || !calledOnce) {
calledOnce = true;
counter = 0;
return func.apply(this, arguments);
}
};
};
我正在用以下方法对其进行测试:
var callBack = function () {
console.log('called');
};
var func = throttle(callback, 1000);
func(); // should be called
setTimeout(func, 500); // should not be called
setTimeout(func, 1000); // should be called
setTimeout(func, 1500); // should not be called
setTimeout(func, 1900); // should not be called
但是,当我按照这里的方式运行代码时,函数只被调用一次,使用原始的func()调用,并且setTimeout中的函数都没有被调用
我的代码或使用setTimeout进行测试是否有任何明显的问题?代码中有什么问题:
var start=newdate();
var i=0,interval=setInterval(函数(){
如果(++i>=1000){
var end=新日期();
var结果=(结束-开始)/1000;
$(“#结果”).text(“平均间隔为”
+结果+“毫秒”);
$('#browser').text(navigator.userAgent);
间隔时间;
}
}, 0);代码>
请等待大约10到20秒
您需要清除间隔,因为无论您是否将间隔设置为零,它们都会增加计数器的值。如果您想调用其他函数,而不是只调用一个设置为false的calledOnce
也许这个代码可以帮助你
这将有助于:
var throttle = function (input, delay) {
var counter = 0;
var calledOnce = false;
var prev = Date.now();
return function () {
var now = Date.now();
counter = now - prev;
if (counter > delay || !calledOnce) {
calledOnce = true;
counter = 0;
prev = now;
return input.apply(null);
}
};
};
一个好的节流功能不需要大量的局部变量。节流功能的目的是减少浏览器资源,而不是应用太多的开销,使您使用的浏览器资源更多。作为这一主张的证据,我设计了一个节流函数,其范围内只有3个“挂起”变量。(一个“挂起”变量是一个永远不会被垃圾收集的变量,因为它总是被一个可能被调用的函数引用,从而占用内存。)请观察
var timenow = self.performance?performance.now.bind(performance):Date.now;
function throttle(func, minInterval) {
var lastTimeWent = -minInterval;
return function() {
var newTimeWent = timenow();
if ((newTimeWent-lastTimeWent) > minInterval) {
lastTimeWent = newTimeWent;
return func.apply(this, arguments);
}
};
}
使用您的示例进行测试:
(函数(){“严格使用”;
var timenow=self.performance?performance.now.bind(performance):Date.now;
功能节流阀(func,最小间隔){
var lasttimeGond=-minInterval;
返回函数(){
var newtimego=timenow();
如果((NewTimeGond LastTimeGond)>minInterval){
LastTimeGond=NewTimeGond;
返回函数apply(这是参数);
}
};
}
var callBack=函数(){
console.log('\tcall!');
};
var func=节流阀(回调,1000);
var start=timenow();
包装器();//应该被调用
setTimeout(包装器,500);//不应调用
setTimeout(包装器,1000);//应被调用
setTimeout(包装器,1500);//不应调用
setTimeout(wrapper,1900);//不应调用
异步函数包装器(){
log(“在“+Math.round(timenow()-start)+”ms..”之后调用”;
func();
log(“完成呼叫!”);
}
})();代码>您只调用一次油门,它设置为calledOnce=true所以下一个func调用引用同一个被调用的throttle,所以它们什么也不做!感谢您的输入,但此功能根本不起作用。另外,我不想清除间隔。我希望计数器继续计数,我只希望它从0重新启动。你的函数从不将calledOnce设置为true,因为你的if语句只检查(!calledOnce)它是否从不失败,这意味着这个节流函数从不节流。我用很长的延迟时间对它进行了测试,并且我能够在没有任何延迟的情况下反复调用它。你的回答非常有用。我可以通过删除setInterval来解决这个问题,而是使用两个新日期()并计算每次调用时都会更新的时间间隔。
var timenow = self.performance?performance.now.bind(performance):Date.now;
function throttle(func, minInterval) {
var lastTimeWent = -minInterval;
return function() {
var newTimeWent = timenow();
if ((newTimeWent-lastTimeWent) > minInterval) {
lastTimeWent = newTimeWent;
return func.apply(this, arguments);
}
};
}