Home  >  Article  >  Web Front-end  >  Detailed explanation of JS timer function examples

Detailed explanation of JS timer function examples

怪我咯
怪我咯Original
2017-06-29 10:46:541604browse

This article mainly introduces the relevant information of javascript timer in detail. It is very comprehensive and detailed. Friends in need can refer to

Timer

Basic format:

The code is as follows:

setInterval(function(){代码},1000);
/*
说明:1.setInterval 会返回一个计时器ID值
   可以这样接收。var setId = setInterval(....);
 2.接收setId的目的是为了清空计时器。
   clearTimeout(setId);
*/

For example


One-time timer

Format:

The code is as follows:

   setTimeout(function(){代码},1000)

Case:

The code is as follows:

        var setId = setTimeout(function(){
         alert('只执行一次');
        },1000);

Title scrolling case

The code is as follows:

setInterval(function(){
     var tit =
document
.title;
    //1~length    +  0  
    document.title = tit.sub
string
(1)+tit.
substr
ing(0,1);
      },1000);

The above is the detailed content of Detailed explanation of JS timer function examples. For more information, please follow other related articles on the PHP Chinese website!

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