setInterval(increment_counter, 1000);
In this java script code , why didnt we use " () " while using function increment_counter?
when i Write the same code with “()” , it only increments the value of counter when cliclked upon.
setInterval(increment_counter, 1000);
In this java script code , why didnt we use " () " while using function increment_counter?
when i Write the same code with “()” , it only increments the value of counter when cliclked upon.
Hello,
The first parameter of the setInterval() function expects a reference to function i.e just increment_counter
.
Whereas increment_counter()
runs the function, and the result of its execution is passed to setInterval
. In our case the result of increment_counter
is undefined
(the function returns nothing), so nothing is scheduled, that is why it only increments the value when you click on it rather than continuously displaying values with a gap of 1 second.
Therefore pass only the reference to function as the first parameter ( i.e just ‘increment_counter’)
Note: If you do write it as increment_counter(), write it within double quotes, like, setInterval(“increment_counter()”, 1000);