Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中的函数调用之间留出一些空间_Javascript_Underscore.js - Fatal编程技术网

在JavaScript中的函数调用之间留出一些空间

在JavaScript中的函数调用之间留出一些空间,javascript,underscore.js,Javascript,Underscore.js,这是我的密码 function func(){ console.log('run') } for(var i=0;i<1000;i++) func() 节流阀对我没有帮助。如果我使用u,我的“func”将不会执行1000次 谢谢。然后创建一个助手函数: -.interval = function(func,ms){ setTimeout(func,ms); // I didn't use setInterval because both aren't same } 我无法想象,

这是我的密码

function func(){
console.log('run')
}

for(var i=0;i<1000;i++)
func()
节流阀对我没有帮助。如果我使用u,我的“func”将不会执行1000次


谢谢。

然后创建一个助手函数:

-.interval = function(func,ms){
   setTimeout(func,ms); // I didn't use setInterval because both aren't same
}
我无法想象,如果不使用更通用的
setTimeout
setInterval
,您为什么要这样做


声明上述内容后,您不需要使用
setTimeout
,而需要使用所谓的helper函数。

还有许多其他方法可以实现这一点,这不是最漂亮的方法,但:

var tick, delay, count, interval, stop;

count = 1000; // 1000 times
delay= 1000;  // 1s between calls

stop = _.after(count,function() {
    clearInterval(interval);
});

tick = function() {
    console.log('TICK');
    stop();
};

interval = setInterval(tick,delay);

为什么不想使用setTimeout或setInterval?如果您发现一个像u.interval这样的函数,您是否检查过它没有使用setTimeout/setInterval??或者您只是使用setTimeout和setInterval失败,现在拒绝使用它?除了命名之外,
.interval(func,1000)
setInterval(func,100)
之间的真正区别是什么?我想第一次执行将在1之后,第二次执行将在2之后,所以需要1000“执行。换句话说,我想要一个helper函数,强制函数在x毫秒内执行不超过1次。但它不会忽略任何调用。哈哈。。。这正是op不想要的。我觉得这很有趣+1这无助于你所做的事。因为func将在一秒钟内执行1000次!我想第一次执行将在1”之后,第二次执行将在2”之后,依此类推。。。
var tick, delay, count, interval, stop;

count = 1000; // 1000 times
delay= 1000;  // 1s between calls

stop = _.after(count,function() {
    clearInterval(interval);
});

tick = function() {
    console.log('TICK');
    stop();
};

interval = setInterval(tick,delay);