setTimeout - callback argument must be a function

后端 未结 4 1452
無奈伤痛
無奈伤痛 2021-01-22 08:07

My code was working until i updated node.js to version 8.11.3

Now i always get error \"callback argument must be a function\" when trying to call a function with setTim

4条回答
  •  猫巷女王i
    2021-01-22 08:49

    Q: I am getting the error "callback argument must be a function" when trying to call a function with setTimeout. Why?

    A: setTimeout(testFunction(itemid, price), 100); You have accidentally pushed the output of testFunction as the first argument of setTimeout which it is suppose to be a function and hence the error.

    What you could do is, pass in a function and call the testFunction recursively from there.

    Example:

    function test(itemid, price) {
        console.log("hello => " + itemid + ", " + price);
        if (price < 50) {
          setTimeout(() => { test(itemid, price + 10) }, 100);    
        }
    }
    
    test(100, 10)

提交回复
热议问题