setTimeout函数delay时间之后执行一次func.
setTimeout函数原型:
var timeoutID = window.setTimeout(func, [delay, param1, param2, ...]); var timeoutID = window.setTimeout(code, [delay]);//not recommended for the same reasons as using eval() //param1, param2, and so forth are additional parameters which are passed through to the function specified by func.
区别:
- 浏览器中:func参数为函数对应的字符串
- Node.js中:func参数为函数名
//In Broswer function f(){ alert("H"); } function f2(){ setTimeout("f()", 2000); } window.onload = f2; //In Node.js function f(){ console.log("H"); } function f2(){ setTimeout(f, 2000); } f2();