Home >Web Front-end >JS Tutorial >js closure implements counting by seconds_javascript skills

js closure implements counting by seconds_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:02:411300browse

Here is a little trick to share with you. Use javascript closure to count by seconds. The code is as follows:

var i =1;
for(i; i<10; i++){
  (function(num){
   setTimeout(function(){
     console.log(num);
     }, num*1000);
   })(i);
}

The focus is on the scope of closures.

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn