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

浅谈jQuery中setInterval()方法_javascript技巧

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

定义和用法:

setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。

setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。

var time=0;

用法1:

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

Copy after login

当需要暂停的时候

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

Copy after login

用法2:

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

Copy after login

当需要暂停时

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

Copy after login

总结:

第一种用法思路比较清晰,先设置一个函数,在通过setInterval来自行调用,但是将其在别处调用比较困难;

第二种方法看起来比较乱,在setInterval内部写下自行调用的函数,然后在给他套上一个有名函数,然后通过调用有名函数来实行自动,在别处调用比较方便。

以上纯属个人看法,希望大神们多多指点。

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!