Merge branch 'patch/JAL-4036_uniprot_fts_legacy_endpoint' into develop
[jalview.git] / examples / javascript / jquery.timer.js
1 \feff/*
2  *
3  *      jQuery Timer plugin v0.1
4  *              Matt Schmidt [http://www.mattptr.net]
5  *
6  *      Licensed under the BSD License:
7  *              http://mattptr.net/license/license.txt
8  *
9  */
10  
11  jQuery.timer = function (interval, callback)
12  {
13  /**
14   *
15   * timer() provides a cleaner way to handle intervals  
16   *
17   *     @usage
18   * $.timer(interval, callback);
19   *
20   *
21   * @example
22   * $.timer(1000, function (timer) {
23   *     alert("hello");
24   *     timer.stop();
25   * });
26   * @desc Show an alert box after 1 second and stop
27   * 
28   * @example
29   * var second = false;
30   *     $.timer(1000, function (timer) {
31   *             if (!second) {
32   *                     alert('First time!');
33   *                     second = true;
34   *                     timer.reset(3000);
35   *             }
36   *             else {
37   *                     alert('Second time');
38   *                     timer.stop();
39   *             }
40   *     });
41   * @desc Show an alert box after 1 second and show another after 3 seconds
42   *
43   * 
44   */
45
46         var interval = interval || 100;
47
48         if (!callback)
49                 return false;
50         
51         _timer = function (interval, callback) {
52                 this.stop = function () {
53                         clearInterval(self.id);
54                 };
55                 
56                 this.internalCallback = function () {
57                         callback(self);
58                 };
59                 
60                 this.reset = function (val) {
61                         if (self.id)
62                                 clearInterval(self.id);
63                         
64                         var val = val || 100;
65                         this.id = setInterval(this.internalCallback, val);
66                 };
67                 
68                 this.interval = interval;
69                 this.id = setInterval(this.internalCallback, this.interval);
70                 
71                 var self = this;
72         };
73         
74         return new _timer(interval, callback);
75  };