var a = 0;
function fn(){
ssss.call(null,a)
// 为什么 定时器里面的匿名函数加上字符串跟直接执行不同???
setInterval("ssss()", 1000);
setInterval(ssss(), 1000);
}
function ssss(){
console.log(++a)
}
fn();
Please ask the master to explain the principle behind it
The first parameter of setInterval accepts a string and will parse the string into a function statement for execution.
First, let’s take a look at W3C’s explanation of setInterval
and then look at it
setInterval("ssss()", 1000);
setInterval(ssss(), 1000);
1. Then an error will be reported when executing;
2. Function body ssss()
There is no return value, but note that there is a sentence ssss.call(null,a) in the fn function, so there is a return value in the fn function. Moreover, the return value is just a function, so it meets the function requirements of setInterval and will continue to be executed
If you don’t add double quotes, you need to remove the parentheses and just write the function name