root/trunk/common/Timer.js

Revision 159, 3.5 kB (checked in by ydnar, 3 years ago)

added line

  • Property svn:keywords set to Id
Line 
1/*
2Timer Library
3$Id$
4
5Copyright (c) 2005, Six Apart, Ltd.
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are
10met:
11
12    * Redistributions of source code must retain the above copyright
13notice, this list of conditions and the following disclaimer.
14
15    * Redistributions in binary form must reproduce the above
16copyright notice, this list of conditions and the following disclaimer
17in the documentation and/or other materials provided with the
18distribution.
19
20    * Neither the name of "Six Apart" nor the names of its
21contributors may be used to endorse or promote products derived from
22this software without specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36*/
37
38
39Timer = new Class( Object, {
40    init: function( callback, delay, count ) {
41        this.callback = callback;
42        this.delay = max( delay, 1 );
43        this.nextDelay = this.delay;
44        this.startTime = 0;
45        this.count = count;
46        this.execCount = 0;
47        this.state = "new";
48        this.timeout = null;
49        this.id = Unique.id();
50        this.start();
51    },
52   
53   
54    destroy: function() {
55        this.stop();
56        this.callback = null;
57    },
58   
59   
60    exec: function() {
61        this.execCount++;
62        this.callback( this );
63        if ( this.count && this.execCount >= this.count ) {
64            return this.destroy();
65        }
66        if( this.state == "started" )
67            this.run();
68    },
69   
70   
71    run: function() {
72        this.state = "started";
73        this.timeout = window.setTimeout( this.exec.bind( this ), this.nextDelay );
74        this.nextDelay = this.delay;
75        var date = new Date();
76        this.startTime = date.UTC;
77    },
78   
79   
80    start: function() {
81        switch( this.state ) {
82            case "new":
83            case "paused":
84            case "stopped":
85                this.run();
86                break;
87        }
88    },
89   
90   
91    stop: function() {
92        this.state = "stopped";
93        try {
94            window.clearTimeout( this.timeout );
95        } catch( e ) {}
96        this.timeout = null;
97    },
98   
99   
100    pause: function() {
101        if( this.state != "started" )
102            return;
103        this.stop();
104        this.state = "paused";
105        var date = new Date();
106        this.nextDelay = max( this.delay - (date.UTC - this.startTime), 1 );
107    },
108
109
110    reset: function( delay ) {
111        if( this.state != "started" )
112            return;
113        if( defined( delay ) )
114            this.delay = this.nextDelay = max( delay, 1 );
115        try {
116            window.clearTimeout( this.timeout );
117        } catch( e ) {}
118        this.timeout = window.setTimeout( this.exec.bind( this ), this.nextDelay );
119    }
120   
121} );
Note: See TracBrowser for help on using the browser.