Comparing version 0.0.2 to 0.0.3
@@ -43,10 +43,3 @@ if (typeof requestAnimationFrame === "undefined") { | ||
function Timer(callback, time) { | ||
this.callback = callback; | ||
this.time = time; | ||
this.flush = false; | ||
this.next = null; | ||
} | ||
// The timer will continue to fire until callback returns true. | ||
@@ -58,3 +51,3 @@ function timer(callback, delay, time) { | ||
// Add the callback to the tail of the queue. | ||
var timer = new Timer(callback, time); | ||
var timer = {callback: callback, time: time, flush: false, next: null}; | ||
if (queueTail) queueTail.next = timer; | ||
@@ -61,0 +54,0 @@ else queueHead = timer; |
@@ -1,1 +0,1 @@ | ||
"undefined"==typeof requestAnimationFrame&&(requestAnimationFrame="undefined"!=typeof window&&(window.msRequestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.oRequestAnimationFrame)||function(e){return setTimeout(e,17)}),function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.timer={})}(this,function(e){"use strict";function t(){l=s=0,c=1/0,n(a())}function n(e){if(!l){var n=e-Date.now();n>24?c>e&&(s&&clearTimeout(s),s=setTimeout(t,n),c=e):(s&&(s=clearTimeout(s),c=1/0),l=requestAnimationFrame(t))}}function i(e,t){this.callback=e,this.time=t,this.flush=!1,this.next=null}function o(e,t,o){o=null==o?Date.now():+o,null!=t&&(o+=+t);var u=new i(e,o);r?r.next=u:m=u,r=u,n(o)}function u(e,t,n){n=null==n?Date.now():+n,null!=t&&(n+=+t),f.callback=e,f.time=n}function a(e){e=null==e?Date.now():+e;var t=f;for(f=m;f;)e>=f.time&&(f.flush=f.callback(e-f.time,e)),f=f.next;f=t,e=1/0;for(var n,i=m;i;)i.flush?i=n?n.next=i.next:m=i.next:(i.time<e&&(e=i.time),i=(n=i).next);return r=n,e}var m,r,f,l,s,c=1/0;e.timer=o,e.timerReplace=u,e.timerFlush=a}); | ||
"undefined"==typeof requestAnimationFrame&&(requestAnimationFrame="undefined"!=typeof window&&(window.msRequestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.oRequestAnimationFrame)||function(e){return setTimeout(e,17)}),function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.timer={})}(this,function(e){"use strict";function n(){f=l=0,c=1/0,t(u())}function t(e){if(!f){var t=e-Date.now();t>24?c>e&&(l&&clearTimeout(l),l=setTimeout(n,t),c=e):(l&&(l=clearTimeout(l),c=1/0),f=requestAnimationFrame(n))}}function i(e,n,i){i=null==i?Date.now():+i,null!=n&&(i+=+n);var o={callback:e,time:i,flush:!1,next:null};m?m.next=o:a=o,m=o,t(i)}function o(e,n,t){t=null==t?Date.now():+t,null!=n&&(t+=+n),r.callback=e,r.time=t}function u(e){e=null==e?Date.now():+e;var n=r;for(r=a;r;)e>=r.time&&(r.flush=r.callback(e-r.time,e)),r=r.next;r=n,e=1/0;for(var t,i=a;i;)i.flush?i=t?t.next=i.next:a=i.next:(i.time<e&&(e=i.time),i=(t=i).next);return m=t,e}var a,m,r,f,l,c=1/0;e.timer=i,e.timerReplace=o,e.timerFlush=u}); |
{ | ||
"name": "d3-timer", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "An efficient queue capable of managing thousands of concurrent animations.", | ||
@@ -29,3 +29,3 @@ "keywords": [ | ||
"test": "faucet `find test -name '*-test.js'`", | ||
"prepublish": "npm run test && uglifyjs build/timer.js -c -m -o build/timer.min.js" | ||
"prepublish": "npm run test && uglifyjs build/timer.js -c -m -o build/timer.min.js && rm -f build/timer.zip && zip -j build/timer.zip -- LICENSE README.md build/timer.js build/timer.min.js" | ||
}, | ||
@@ -32,0 +32,0 @@ "devDependencies": { |
@@ -17,20 +17,47 @@ # d3-timer | ||
Schedules a new timer, invoking the specified *callback* repeatedly until it returns true. To cancel the timer after it starts, the *callback* must return a truthy value. | ||
Schedules a new timer, invoking the specified *callback* repeatedly until it returns true. (There is no API for canceling a timer; the *callback* must return a truthy value to terminate.) An optional numeric *delay* in milliseconds may be specified to invoke the given *callback* after a delay. The delay is relative to the specified *time* in milliseconds since UNIX epoch; if *time* is not specified, it defaults to [Date.now](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/now). | ||
An optional numeric *delay* in milliseconds may be specified to invoke the given *callback* after a delay. The delay is relative to the specified *time* in milliseconds since UNIX epoch; if *time* is not specified, it defaults to [Date.now](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/now). | ||
The *callback* is passed two arguments each time it is invoked: the elapsed time since the timer became active, and the current time. The latter is useful for precise scheduling of secondary timers. For example: | ||
```js | ||
timer(function(elapsed, time) { | ||
console.log(elapsed, time); | ||
return elapsed > 200; | ||
}, 150); | ||
``` | ||
This produced the following console output: | ||
``` | ||
6 1433806724202 | ||
26 1433806724222 | ||
51 1433806724247 | ||
73 1433806724269 | ||
92 1433806724288 | ||
114 1433806724310 | ||
132 1433806724328 | ||
152 1433806724348 | ||
171 1433806724367 | ||
191 1433806724387 | ||
213 1433806724409 | ||
``` | ||
Note that the first *elapsed* is 6ms, since this is the elapsed time since the timer started, not the elapsed time since the timer was scheduled. | ||
Use *delay* and *time* to specify relative and absolute moments in time when the *callback* should start being invoked. For example, a calendar notification might be coded as: | ||
```js | ||
timer(callback, -4 * 1000 * 60 * 60, +new Date(2012, 09, 29)); // four hours before midnight October 29 (months are zero-based) | ||
timer(callback, -4 * 1000 * 60 * 60, new Date(2012, 09, 29)); // four hours before midnight October 29 (months are zero-based) | ||
``` | ||
Note that if [timer](#timer) is called within the callback of another timer, the new timer callback (if eligible as determined by the specified *delay* and *time*) will be invoked immediately at the end of the current frame, rather than waiting until the next frame. | ||
If [timer](#timer) is called within the callback of another timer, the new timer callback (if eligible as determined by the specified *delay* and *time*) will be invoked immediately at the end of the current frame, rather than waiting until the next frame. | ||
<a name="timerFlush" href="#timerFlush">#</a> <b>timerFlush</b>() | ||
<a name="timerFlush" href="#timerFlush">#</a> <b>timerFlush</b>([<i>time</i>]) | ||
Immediately execute (invoke once) any eligible timer callbacks. Zero-delay timers are normally first executed after one frame (~17ms). This can cause a brief flicker because the browser renders the page twice: once at the end of the first event loop, then again immediately on the first timer callback. By flushing the timer queue at the end of the first event loop, you can run any zero-delay timers immediately and avoid the flicker. | ||
Immediately execute (invoke once) any eligible timer callbacks. If *time* is specified, it represents the current time; if not specified, it defaults to Date.now. Specifying the time explicitly can ensure deterministic behavior. | ||
Note that zero-delay timers are normally first executed after one frame (~17ms). This can cause a brief flicker because the browser renders the page twice: once at the end of the first event loop, then again immediately on the first timer callback. By flushing the timer queue at the end of the first event loop, you can run any zero-delay timers immediately and avoid the flicker. | ||
<a name="timerReplace" href="#timerReplace">#</a> <b>timerReplace</b>(<i>callback</i>[, <i>delay</i>[, <i>time</i>]]) | ||
Replace the current timer’s *callback*, *delay* and *time*. This method can only be called within a timer callback, and is equivalent to [timer](#timer), except that it replaces the current timer rather than scheduling a new timer. |
@@ -14,3 +14,3 @@ var queueHead, | ||
// Add the callback to the tail of the queue. | ||
var timer = new Timer(callback, time); | ||
var timer = {callback: callback, time: time, flush: false, next: null}; | ||
if (queueTail) queueTail.next = timer; | ||
@@ -64,9 +64,2 @@ else queueHead = timer; | ||
function Timer(callback, time) { | ||
this.callback = callback; | ||
this.time = time; | ||
this.flush = false; | ||
this.next = null; | ||
} | ||
function wake() { | ||
@@ -73,0 +66,0 @@ frame = timeout = 0, timeoutTime = Infinity; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13839
63
170