最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How would I toggle the state of a setInterval function in jQuery? - Stack Overflow

programmeradmin1浏览0评论

I want to be able to click a an element with an id of pause to start a count of the elements in a time object and if I re click the pause it will stop it and reclick start it exactly like the toggle feature in JQuery but with a setInteval function how would I go about doing this?

$("#pause").click(function(ffe) {
  if(on == true) {
    on = false 
    alert("on");
  }
  else {
    on = true;
    alert("off");
  }
  if(on == false) {
    setInterval(function() {
      $("#timet ul").append("<li>" + $("#time ul")
                    .children('li').length +"</li>");

    }, 100);
  }
  else {

    alert("Error");
  }
});

I want to be able to click a an element with an id of pause to start a count of the elements in a time object and if I re click the pause it will stop it and reclick start it exactly like the toggle feature in JQuery but with a setInteval function how would I go about doing this?

$("#pause").click(function(ffe) {
  if(on == true) {
    on = false 
    alert("on");
  }
  else {
    on = true;
    alert("off");
  }
  if(on == false) {
    setInterval(function() {
      $("#timet ul").append("<li>" + $("#time ul")
                    .children('li').length +"</li>");

    }, 100);
  }
  else {

    alert("Error");
  }
});
Share Improve this question edited Aug 8, 2012 at 3:21 elclanrs 94.2k21 gold badges137 silver badges171 bronze badges asked Aug 8, 2012 at 3:18 AaronAaron 11.7k18 gold badges61 silver badges73 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

A classic technique is to use a single master setInterval loop and simply use if..else logic to determine what needs to run. This is how a lot of javascript games work:

var on = true;

// Our master scheduler:
setInterval(function() {
    if (on) {
      $("#timet ul").append("<li>" + $("#time ul")
         .children('li').length +"</li>");
    }
}, 100);

// Code to handle the pause button
$("#pause").click(function(ffe) {
    on = !on;
}

You can use the setTimeout function, if you want to run the function once, setInterval runs continuously, try the following:

var on = false;
$("#pause").click(function(ffe) {
  if (on) {
      on = false;
      setTimeout(function() {
           $("#timet ul").append("<li>" + $("#time ul")
                    .children('li').length +"</li>");
      }, 100);
  } else {
      on = true; 
  }
});

You need to use .clearInterval() to stop the execution.

Here is the code: (THE WORKING DEMO)

$("#pause").click((function () {
    var interId = null;
    var $ul = $("#timet ul");
    return function (e) {
        if (interId) {
            $(this).text("start");
            clearInterval(interId);
            interId = null;
        } else {
            $(this).text("pause");
            interId = setInterval(function () {
                $ul.append($('<li>').text($('li', $ul).length));
            }, 100);
        }
    };
}()));​
发布评论

评论列表(0)

  1. 暂无评论