Why can't methods passed in by js's setInterval method have parentheses?

var sessionexpiretime = setInterval (checkSessionIsExpire, 1000); can
var sessionexpiretime = setInterval (checkSessionIsExpire (), 1000); execute only once

Dec.17,2021

if you want to understand what parentheses mean, you can execute the following code

function getVal() {
  console.log('In getVal')
  return 123
}
console.log(getVal)
console.log(getVal())

the result is

[Function: getVal]
In getVal
123

means that the unparenthesized function name itself getVal represents the function. If you add parentheses, it becomes a call to the function, and the value of the expression becomes the return value of the function after execution.


is executed with parentheses. If your function returns a method, it will continue to execute the method, if not, it is like writing a undefined


first of all, the first parameter of setInterval is a function, which is a callback function that needs to be executed by setInterval, and it is up to setInterval to decide when to execute it (of course, basically, it is still determined by the second parameter you pass in). If you bring (), it means that you execute it yourself, instead of giving it to setInterval to execute, and the first parameter you pass in is just a value after function execution, not function itself.
ps: garrulous sentence, the foundation of js is very poor, it is suggested that the system sort out the foundation of js, and then consider others.


to put it simply, the first parameter of setInterval is a callback function, you add a parenthesis to pass in the return value of the function rather than the function itself, and execute it only once because the function itself executes when you call setInterval instead of the timer that drives the function to execute.


basics, you don't have to go too deep into it. You can execute it with parentheses. Without parentheses, you have passed in the method. The method name is followed by parentheses to indicate the execution method!


  

the function is executed with parentheses, and the unaccompanied representative passes the function name as an argument into

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a689-2c225.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a689-2c225.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?