Code Monkey home page Code Monkey logo

timed's Introduction

#Timed

Timed provides syntactic sugar around JavaScript's native setTimeout and setInterval functions.

To execute some code in 5 minutes, instead of writing:

setTimeout(function() { ... }, 300000); // how many zeros is that?

or the slightly more readable:

setTimeout(function() { ... }, 5 * 60 * 1000); // 5... (multiplies in head) min

now you can just write:

Timed.after(5, "minutes", function() { ... });

setTimeout becomes Timed.after and setInterval becomes Timed.every.

.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.

These two functions both return this to allow for chaining.

###.yield()

Timed also provides a simple function yield processing time to the browser before executing the next block of code.

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:

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() { ... }); // 2 hours, 45 minutes
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.

Valid time units include:

  • millisecond(s) (default)

  • ms

  • second(s)

  • sec(s)

  • s

  • minute(s)

  • min(s)

  • m

  • hour(s)

  • hr(s)

  • h

  • day(s)

  • d

##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:

    $.extend($, Timed.noConflict());

##Meta

Licensed under the 3-clause BSD license (BSD_LICENSE.txt)

Copyright (c) 2011, Jarvis Badgley (chipersoft at gmail), Arthur Klepchukov (at gmail)

timed's People

Contributors

avk avatar twipped avatar janhartigan avatar fschaefer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.