Javascript: timer usage
In Javascript, the setTimeout and clearTimeout functions are used for timing/stop timing.
1. Execute an action after the specified time. For example, a dialog box is displayed after 3 s:
SetTimeout ('alert ("3 s") ', 5000 );
In addition, this function can be used for superposition, for example:
Function delay_times () {setTimeout ('document. getElementById ("time_text "). innerHTML = "2 s" ', 2000); setTimeout ('document. getElementById ("time_text "). innerHTML = "4 s" ', 4000); setTimeout ('document. getElementById ("time_text "). innerHTML = "6 s" ', 6000 );}
2. example of how to start counting and stop counting:
Var count = 0; var myTimer; // start counting function start_counter () {document. getElementById ("time_count "). innerHTML = count; count ++; myTimer = setTimeout ("start_counter ()", 1000) ;}// stop counting function stop_counter () {clearTimeout (myTimer );}