setTimeout方法注意事项

Passing string literalsSection

Passing a string instead of a function to setTimeout() has the same associated problems as using eval.

// Recommended
window.setTimeout(function() {
  alert('Hello World!');
}, 500);

// Not recommended
window.setTimeout("alert('Hello World!');", 500);

A string passed to setTimeout is evaluated in the global context, so local symbols in the context where setTimeout() was called will not be available when the string is evaluated as code.

评论

暂无评论