简体   繁体   English

在setTimeout中调用一个函数

[英]calling a function within setTimeout

Defining a function and calling "callback" 定义函数并调用“回调”

var evenDoubler = function(number, callback) {
    if (number % 2 == 0) {
        setTimeout(function() {
             callback(null, number * 2)
        }, 100);
    }
}

Directly calling "callback" 直接调用“回调”

var evenDoubler = function(number, callback) {
    if (number % 2 == 0) {
        setTimeout(callback(null, number * 2), 100);
    }
}

Whats the difference between these 2 approaches? 这两种方法有什么区别?

When you "directly" call callback() , that happens before the timer goes off; 当您“直接”调用callback() ,这发生计时器关闭之前 it happens before the call to setTimeout() happens. 它发生在对setTimeout()的调用发生之前。

An expression like 像这样的表达

callback(null, number * 2)

is a function call, and as such it's always evaluated when JavaScript needs its value. 是一个函数调用,因此,它总是在JavaScript需要其值时进行评估。 When it's used in a function call: 在函数调用中使用它时:

setTimeout(callback(null, number * 2), 100);

then it's evaluated in order to get the value that needs to be passed to setTimeout() . 然后对其求值,以获取需要传递给setTimeout() It's the same as if you call some function with a simple arithmetic expression: 就像您使用简单的算术表达式调用某些函数一样:

someFunction(x + y);

You fully expect that what's passed to the function is the sum of x and y . 你完全相信,什么传递给函数的总和 xy A function call is just another sort of expression, and it too will be fully evaluated when it's used as an argument to a function. 函数调用只是另一种表达式,当将其用作函数的参数时,也会对其进行完全评估。

setTimeout takes as first argument a function reference. setTimeout将函数引用作为第一个参数。 So when you call a function (as in your second case), that function call should return a function. 因此,当您调用函数时(如第二种情况),该函数调用应返回一个函数。 This is not your case. 这不是你的情况。 You want to pass the function reference -- so don't call it. 您想传递函数引用-不要调用它。 You could do it with bind which does return a new function, with the arguments already bound to it: 您可以使用bind来执行此操作, bind确实会返回一个新函数,并且已经绑定了参数:

var evenDoubler = function(number, callback) {
    if (number % 2 == 0) {
        setTimeout(callback.bind(null, null, number * 2), 100);
    }
}

Question is already answered but I want to chime in as I have tested this solution when I was going through the same course. 问题已经回答,但是我想通过参加相同课程的测试该解决方案来说明一下。

If you really want to do the "dirty" call in a neat way then give the first argument as just the function name, rest of the parameters after timeout period, as explained here: 如果您确实想以一种简洁的方式进行“脏”调用,则将第一个参数作为函数名,在超时时间段后其余参数作为参数提供,如下所示:

https://nodejs.org/en/docs/guides/timers-in-node/ https://nodejs.org/en/docs/guides/timers-in-node/

going by above explanation I tested the code with below and worked: 通过上面的解释我用下面的代码测试并工作:

var eventDoubler = function (num, callback) {
    var waitTime = Math.floor(Math.random()*(maxTime+1));

    if( num % 2 ) {
        setTimeout(callback, waitTime, new Error('Odd Input'));
    } else {
        setTimeout(callback, waitTime, null, num*2 , waitTime );
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM