setInterval方法抽离后传参后只执行一次
setInterval(hello('nihao'), 30);
function hello(word) {
console.log(word)
}
如果像上面一样传递参数 调试发现只执行了一次
这是因为 hello(‘nihao’)相当于函数调用了一次 不是回调函数的正确写法
查阅后我们可以看到
declare function setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
可以看到后面的arguments:,这才是我们传参的地方setInterval(methodName, time, xxx);
setInterval(hello, 30, 'nihao');
function hello(word) {
console.log(word)
}
dName, time, xxx);**
setInterval(hello, 30, 'nihao');
function hello(word) {
console.log(word)
}