Timer setTimeout () and setinterval () are two very important and useful methods of native Js. However, many people mistakenly think that they are using the same function: Execute the incoming handle function repeatedly at intervals. but in fact, this is not the case. Since JS provides two different names, there must be some differences.
Let's take a look at the two JS manuals and their explanations in the English dictionary:
JS manual» setTimeout (): used to call a function or computing expression after a specified number of milliseconds;
» Timeout (): Time-out; temporary break; Inter-work break;
JS manual» setinterval (): Call a function or computing expression according to the specified period (in milliseconds). The function will be called continuously until clearinterval () is called or the window is closed;
» Interval (): interval; spacing; interscreen rest;
It is not hard to see that as long as we carefully understand the JS manual and naming definitions, we can easily distinguish the two. Simply put, the difference between the two lies in,The setTimeout () method is to execute the function after a specified time and only execute the incoming handle function once. the setinterval () method is to execute the incoming handle function once after each specified interval, and execute it cyclically until the window is closed or clearinterval ().
To make it easier for obfuscators to understand, I wrote an example specifically:Click here to view the demo
As for usage, you don't have to say much. After understanding the differences between the two, you should know how to choose between them.
@ Popular Article
- Recruit part-time web front-end developers 1 ~ 2 [currently available]
- Details about jquery queue control methods: Queue ()/dequeue ()/clearqueue ()
- Structure/performance/behavior separation tab (JQ and native JS)
- Difference, not a little bit
- Abbreviated getelementsbytagname
Clearinterval, cleartimeout, JS timer, setinterval, setTimeout
Blog published in mr. Think: difference between JS timer setTimeout () and setinterval () @ mr. Think