ncr / at_intervals

A simple jQuery plugin for dealing with setIntervals and clearIntervals

This URL has Read+Write access

at_intervals / example.html
100644 70 lines (57 sloc) 1.974 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<html>
<head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
  <script type="text/javascript" src="./jquery.at_intervals.js"></script>
</head>
<body>
  <h1>Observe the Firebug Console</h1>
 
  <a href="#" id="pause">Pause</a> /
  <a href="#" id="resume">Resume</a><br />
  <a href="#" id="stop">Stop</a><br />
  <a href="#" id="start_another">Start another</a><br />
  <a href="#" id="toggle_visibility">Toggle visibility (pauses/resumes the intervals)</a><br />
  <a href="#" id="remove_from_dom">Remove widget from DOM (stops the intervals)</a><br />
 
  <script type="text/javascript">
    $("#remove_from_dom").click(function(){
      $("#tagsoup1").empty()
      return false
    });
 
    $("#toggle_visibility").click(function(){
      $("#tagsoup1").toggle()
      return false
    });
 
    $("#pause").click(function(){
      $("#widget").data("foo").should_pause = true
      return false
    });
 
    $("#resume").click(function(){
      $("#widget").data("foo").should_pause = false
      return false
    });
 
    $("#stop").click(function(){
      $("#widget").data("foo").should_stop = true
      return false
    });
    
    $("#start_another").click(function(){
      $("#widget").at_intervals(function(){
        console.log("Doing some other work now...")
      }, { name: "foo", delay: 500 })
      return false
    })
  </script>
 
  <div id="tagsoup1">
    <div id="tagsoup2">
 
      <div id="widget" style="border: 1px dotted gray; margin: 2em; padding: 1em; height: 150px">
        <p>A widget. Self-contained piece of code and looks.</p>
        <div id="logic" style="background-color: red; width: 66px; height: 66px"></div>
 
        <script type="text/javascript">
          $("#widget").at_intervals(function(){
            console.log("Working...")
            $("#logic").toggle(200)
          }, { name: "foo" });
        </script>
      </div>
 
    </div>
  </div>
 
</body>
</html>