当前位置: 代码迷 >> Web前端 >> setTimeout()跟setInterval()的区别
  详细解决方案

setTimeout()跟setInterval()的区别

热度:709   发布时间:2012-11-14 10:12:19.0
setTimeout()和setInterval()的区别
setTimeout or setInterval?

Why do I use setTimeout, not setInterval? In my opinion, the two methods, though similar, are meant for different situations, especially when it comes to animations. Before continuing, let's review the difference:

setTimeout('theFunction()',100);
setInterval('theFunction()',100);

?

The first line means "Execute function theFunction() 100 milliseconds from now." The second line means "Execute function theFunction() 100 milliseconds from now, and continue executing it every 100 milliseconds."

setInterval is best for animations that continue for an indefinite time (for instance, until the user does something). setTimeout, on the other hand, is best suited for animations that have a fixed start and end point.