Node.js callback function question.

Asked 2 years ago, Updated 2 years ago, 119 views

var i = 0; function a(callback){ i +=1; callback(i); }; setInterval(a((num)=>{ console.log(num); }),1000);

The above is the code I made. I expected that it would come out as 1, 2, 3... every second, but with the error TypeError: "callback" argument must be a function, only 1 is printed and finished.

Why is this happening?

node.js callback

2022-09-21 17:21

1 Answers

setInterval() The first factor that the function receives should be a function, but it contains the contents of the function. It works by enclosing a((num)=>{ console.log(num);}) with an anonymous function.

demo


2022-09-21 17:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.