如何停止jQuery脚本 [英] How to stop a jQuery script

查看:102
本文介绍了如何停止jQuery脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用指定的时间间隔显示和隐藏div jQuery

Possible Duplicate:
Show and hide divs at a specific time interval using jQuery

我有一个jQuery脚本,它以特定的时间间隔显示和隐藏div.它工作正常,但我有一个问题,它不断显示和隐藏div,因此基本上它永远不会停止. 我该如何停止?可以说它在显示3倍的div后停止.

I have a jQuery script what is showing and hiding divs at a specific interval. It's working fine but I have one problem, its showing and hiding divs round and round so basically its never stops. How can I make this stop? Lets say it stops after showing 3x the divs.

$(function() {
    var timer = setInterval( showDiv, 5000);    
    var counter = 0;

    function showDiv() {
        if (counter == 0) { 
            counter++; 
            return; 
        }

        $('div','#container')
            .stop()
            .hide()
            .filter( function() { return this.id.match('div' + counter); })   
            .show('fast');

        counter == 3? counter = 0 : counter++; 
    }
});

推荐答案

尝试一下:

$(function() {

  var timer = setInterval( showDiv, 5000);

  var counter = 0;
  var showTimes = 3;
  function showDiv() {
    if (counter ==0) { counter++; return; }

    $('div','#container')
      .stop()
      .hide()
      .filter( function() { return this.id.match('div' + counter); })   
      .show('fast');

    if(counter == 3){
       counter = 0;
       showTimes--;
       if(showTimes == 0)
           clearInterval(timer);
    }else{
       counter++; 
    }

  }

});

这篇关于如何停止jQuery脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆