Home > Web Front-end > JS Tutorial > body text

js closure implements counting by seconds_javascript skills

WBOY
Release: 2016-05-16 16:02:41
Original
1270 people have browsed it

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);
}

Copy after login

The focus is on the scope of closures.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!