Home > Web Front-end > JS Tutorial > A brief discussion on the setInterval() method in jQuery_javascript skills

A brief discussion on the setInterval() method in jQuery_javascript skills

WBOY
Release: 2016-05-16 15:51:00
Original
1270 people have browsed it

Definition and usage:

The setInterval() method can call a function or calculate an expression according to the specified period (in milliseconds).

The setInterval() method will keep calling the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.

var time=0;

Usage 1:

function jump(){
  …………  //函数内容
}
time = setInterval("jump",5000); //每个五秒调用一次函数

Copy after login

When you need to pause

  $("").hover(function(){
    clearInterval(time),function(){
    time = setInterval("jump",5000); 
    }  
  })

Copy after login

Usage 2:

function autoPlay(){
  time = setInterval(function(){
    …………   //函数内容
  },5000);
}
autoPlay();  //调用函数

Copy after login

When you need to pause

   $("").hover(function(){
    clearInterval(time),function(){
    autoPlay();
    }  
  })

Copy after login

Summary:

The first usage idea is clearer. First set up a function and call it yourself through setInterval, but it is more difficult to call it elsewhere;

The second method seems messy. Write the self-calling function inside setInterval, then attach a famous function to it, and then implement automation by calling the famous function. It is more convenient to call it elsewhere.

The above is purely my personal opinion, I hope the experts can give me some advice.

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