Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the .yield(callback) event for executing code on the next event…

… loop cycle.
  • Loading branch information...
commit f0245c6db06f4dbaf3b0380a124fb35e86de5714 1 parent 124062b
@ChiperSoft authored
Showing with 52 additions and 29 deletions.
  1. +32 −25 README.md
  2. +16 −1 timed.js
  3. +4 −3 timed.min.js
View
57 README.md
@@ -1,5 +1,4 @@
-Timed
-====================
+#Timed
Timed provides syntactic sugar around JavaScript's native
`setTimeout` and `setInterval` functions.
@@ -7,48 +6,57 @@ Timed provides syntactic sugar around JavaScript's native
To execute some code in 5 minutes, instead of writing:
```js
- setTimeout(function() { ... }, 300000); // how many zeros is that?
+setTimeout(function() { ... }, 300000); // how many zeros is that?
```
or the slightly more readable:
```js
- setTimeout(function() { ... }, 5 * 60 * 1000); // 5... (multiplies in head) min
+setTimeout(function() { ... }, 5 * 60 * 1000); // 5... (multiplies in head) min
```
now you can just write:
```js
- Timed.after(5, "minutes", function() { ... }); // :)
+Timed.after(5, "minutes", function() { ... });
````
`setTimeout` becomes `Timed.after` and `setInterval` becomes `Timed.every`.
-Both functions return a timer object which contains the calculated interval, a reference to the callback, the setTimeout/setInterval handle, and two control functions:
+.every() and .after() both return a timer object which contains the calculated interval, a reference to the callback, the setTimeout/setInterval handle, and two control functions:
- `timer.cancel()`: Cancels the timer and nulls timeout id.
- `timer.start()`: Restarts the timer after it has been canceled, using the same callback as before.
-Both functions return `this` to allow for chaining.
+These two functions both return `this` to allow for chaining.
-Usage
------
+###.yield()
+
+Timed also provides a simple function yield processing time to the browser before executing the next block of code.
+
+```js
+Timed.yield(function() { ... });
+```
+
+This takes advantage of the `window.postMessage()` feature (when available) to execute the block as soon as the browser hands control back to JavaScript, often yielding faster results than a setTimeout can. In older browsers, .yield() falls back to a 0 millisecond timeout.
+
+##Usage
These are all valid calls:
```js
- Timed.after(100, function() { ... }); // 100 milliseconds
- Timed.after("9.7", function() { ... }); // 9.7 milliseconds
- Timed.after("50sec", function() { ... }); // 50 seconds
- Timed.after(7, "mins", function() { ... }); // 7 minutes
- Timed.after("33", "hours", function() { ... }); // 33 hours
- Timed.after("2 hours, 45 minutes", function() { ... }); // 33 hours
- Timed.after("minute", function() { ... }); // 1 minute
- Timed.after([
- ["1", "minute"],
- [34, "seconds"],
- "100 milliseconds"
- ], function() { ... });
+Timed.after(100, function() { ... }); // 100 milliseconds
+Timed.after("9.7", function() { ... }); // 9.7 milliseconds
+Timed.after("50sec", function() { ... }); // 50 seconds
+Timed.after(7, "mins", function() { ... }); // 7 minutes
+Timed.after("33", "hours", function() { ... }); // 33 hours
+Timed.after("2 hours, 45 minutes", function() { ... }); // 33 hours
+Timed.after("minute", function() { ... }); // 1 minute
+Timed.after([
+ ["1", "minute"],
+ [34, "seconds"],
+ "100 milliseconds"
+], function() { ... });
```
`Timed.every`, for creating intervals, has the same exact syntax as `$.after`.
@@ -73,8 +81,8 @@ Valid time units include:
* day(s)
* d
-noConflict & jQuery
-=====
+##noConflict & jQuery
+
Timed supports calling the `.noConflict()` function to remove the Timed namespace. `noConflict()` will then return the Timed object for assignment to a new location.
If you wish to remap Timed's functions into jQuery's global object it can be done like so:
@@ -84,8 +92,7 @@ If you wish to remap Timed's functions into jQuery's global object it can be don
```
-Meta
-----
+##Meta
_Licensed under the 3-clause **BSD license** (BSD_LICENSE.txt)_
View
17 timed.js
@@ -5,7 +5,7 @@
* Licensed under the BSD license (BSD_LICENSE.txt)
*
* @author <a href="mailto:chipersoft@gmail.com">Jarvis Badgley</a>
- * @version 1.2
+ * @version 1.3
*/
@@ -92,6 +92,21 @@
var timer = create_timer.apply(this, arguments);
timer.type = 'i';
return timer.start();
+ },
+
+ yield : function yield(callback) {
+ if (!!window.postMessage && !!window.addEventListener) {
+ //uses the postMessage feature when available. postMessage events fire before the timeout loop triggers
+ var wrapper, id = Math.round(Math.random()*1000000);
+ window.addEventListener('message', wrapper = function () {
+ window.removeEventListener('message', wrapper);
+ if (event.data === id) callback();
+ });
+ window.postMessage(id, '*');
+ } else {
+ //setTimeout fallback for browsers that don't support postMessage.
+ Timed.after(0,'ms', callback);
+ }
}
};
View
7 timed.min.js
@@ -5,9 +5,10 @@
* Licensed under the BSD license (BSD_LICENSE.txt)
*
* @author <a href="mailto:chipersoft@gmail.com">Jarvis Badgley</a>
- * @version 1.2
+ * @version 1.3
*/
-(function(d){function g(){if(this.id&&clearInterval(this.id))this.id=null;return this}function h(){this.id=this.type=="i"?setInterval(this.callback,this.when):setTimeout(this.callback,this.when);return this}function e(){var c={when:null,callback:null,cancel:g,start:h},b=arguments.length;c.callback=arguments[b-1];if(typeof c.callback!=="function")throw"Timed.after and Timed.every - Require a callback as the last argument";var a=(arguments[0]instanceof Array?arguments[0].join(""):String(arguments[0]))+
+(function(d){function h(){if(this.id&&clearInterval(this.id))this.id=null;return this}function i(){this.id=this.type=="i"?setInterval(this.callback,this.when):setTimeout(this.callback,this.when);return this}function f(){var c={when:null,callback:null,cancel:h,start:i},b=arguments.length;c.callback=arguments[b-1];if(typeof c.callback!=="function")throw"Timed.after and Timed.every - Require a callback as the last argument";var a=(arguments[0]instanceof Array?arguments[0].join(""):String(arguments[0]))+
(b===3?String(arguments[1]):"");if(isNaN(parseInt(a,10)))a="1"+a;a=a.toLowerCase().replace(/[^a-z0-9\.]/g,"").match(/(?:(\d+(?:\.\d+)?)(?:days?|d))?(?:(\d+(?:\.\d+)?)(?:hours?|hrs?|h))?(?:(\d+(?:\.\d+)?)(?:minutes?|mins?|m))?(?:(\d+(?:\.\d+)?)(?:seconds?|secs?|s))?(?:(\d+(?:\.\d+)?)(?:milliseconds?|ms))?/);if(a[0])c.when=parseFloat(a[1]||0)*86400000+parseFloat(a[2]||0)*3600000+parseFloat(a[3]||0)*60000+parseFloat(a[4]||0)*1000+parseInt(a[5]||0,10);else if(b===3||!(c.when=parseInt(arguments[0],10)))throw"Timed.after and Timed.every - Could not parse delay arguments, check your syntax";
-return c}var f={after:function c(){var b=e.apply(this,arguments);b.type="t";return b.start()},every:function c(){var b=e.apply(this,arguments);b.type="i";return b.start()}},i=d.Timed;f.noConflict=function(){d.Timed=i;return this};d.Timed=f})(this);
+return c}var e={after:function c(){var b=f.apply(this,arguments);b.type="t";return b.start()},every:function c(){var b=f.apply(this,arguments);b.type="i";return b.start()},yield:function c(b){if(window.postMessage&&window.addEventListener){var a,g=Math.round(Math.random()*1000000);window.addEventListener("message",a=function(){window.removeEventListener("message",a);event.data===g&&b()});window.postMessage(g,"*")}else e.after(0,"ms",b)}},j=d.Timed;e.noConflict=function(){d.Timed=j;return this};d.Timed=
+e})(this);
Please sign in to comment.
Something went wrong with that request. Please try again.