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.

你可能感兴趣的:(SetInterval)