Why can I not pass a function call (rather than a function reference or an anonymous function) to setTimeout()?

前端 未结 5 584
庸人自扰
庸人自扰 2020-12-07 05:52

Please ignore the fact that this code achieves nothing and apologies for what\'s probably an inane question!

I understand that I cannot pass a function call into

5条回答
  •  -上瘾入骨i
    2020-12-07 06:26

    The correct way of passing a function reference is to use callbacks.

    names.forEach(name => setTimeout(function() {
        printer(name);
    }, 1000));
    

    callbacks contains reference to the function.

    setTimeout(callbackFunction, milliseconds);
    

提交回复
热议问题