Using js to write a countdown, it is estimated that many of our friends will come up with the setinterval (FN, millisec) function. But what if I don't need this function? Can you think of other methods? Hey, this is the setTimeout function,
These two functions are two very important functions for animation in Javascript!
First, check the Code:
1 var countdown = function (times) {2 if (! Times | isnan (parseint (times) return; 3 var ARGs = arguments; 4 var self = This; 5 console. log (times); 6 setTimeout (function () {args. callee. call (self, -- times) }, 1000); 7 8}
Call countdown (10). The countdown starts from 10 on the chrome console! Although it looks easy and there are not many codes, it involves understanding JS single-thread. If you do not understand JS single-thread, you may encounter many pitfalls in the future,
We hope that our partners can learn more and consolidate their basic knowledge so that they can practice deep internal skills and kill people in an invisible way!
Implement countdown using setTimeout