setInterval and setTimeout should be used with callbacks, for example:
setInterval(timer, 1000);
or unnamed functions:
setInterval( function() { console.log("timer!"); }, 1000 );
Why does your code not work - when you pass a function as an argument to another function using brackets, for example. doSomething ( someFunc() ) you pass the result to the function.
When a function is passed as an object, for example. doSomething ( someFunc ) you pass the callback. This someFunc method is passed as a reference and executed somewhere in the calling function. This is the same as function pointers in other languages.
A common mistake is to use these two functions, as shown on w3schools . This makes an implicit call to eval .
Bakudan Apr 17 2018-12-12T00: 00Z
source share