Permalink
Browse files

Release 0.2.0

  • Loading branch information...
1 parent 437e385 commit d0035b3836cfbfb199f4024d950d827d71e47a1a @elving committed Oct 20, 2012
Showing with 356 additions and 18 deletions.
  1. +36 −4 README.md
  2. +223 −0 test/wait_test.js
  3. +97 −14 wait.js
View
@@ -1,7 +1,7 @@
# wait
Syntactic sugar for setTimeout and setInterval.
-## wait(time, callback)
+## wait(time, callback, id)
Sugar for setTimeout.
@@ -12,8 +12,12 @@ Sugar for setTimeout.
wait('2s', function(){
// Do something in 2 seconds
});
+
+ wait('2 minutes', function(){
+ // Do something in 2 seconds
+ }, 'awesomeId');
-## repeat(time, callback, callBefore = false)
+## repeat(time, callback, id, callBefore = false)
Sugar for setInterval.
@@ -25,13 +29,17 @@ If `callBefore` is true the callback will be executed before the interval.
repeat('1 min', function() {
// Do something every minute
- });
+ }, 'awesomeId');
repeat('1 min', function() {
// Run immediately and every minute
}, true);
+
+ repeat('1 min', function() {
+ // Run immediately and every minute
+ }, 'awesomeId', true);
-## until(condition, callback, interval = 100)
+## until(condition, callback, interval = 100, id)
Runs the callback function when the condition is true. If you don't specify an interval the condition will be check every 100 milliseconds.
@@ -46,6 +54,30 @@ Runs the callback function when the condition is true. If you don't specify an i
until(condition, function() {
// Check every second if 1 == 1 and do something.
}, '1 second');
+
+ until(condition, function() {
+ // Check every second if 1 == 1 and do something.
+ }, 150, 'coolId');
+
+## clear
+
+The `clear()` method can be used in 3 ways:
+
+ // Will clear all intervals and timeouts that where created with an id
+ clear();
+
+ // Will clear a timeout or interval given an id.
+ clear(id);
+
+ // The id can be two things.
+ var timeout = wait(100, callback);
+ clear(timeout);
+
+ wait(100, callback, 'awesome-id');
+ clear('awesome-id');
+
+ // Will clear all timeouts and intervals in an array.
+ clear([timeout, 'awesome-id']);
## time
View
@@ -100,3 +100,226 @@ describe('until', function() {
}
});
});
+
+describe('clear', function() {
+ it('should clear the timeout.', function(done) {
+ var count = 0,
+ timeout = 0;
+
+ timeout = wait('1s', function() {
+ count = 1;
+ });
+
+ wait(500, function() {
+ clear(timeout);
+ });
+
+ wait('2s', function() {
+ count.should.equal(0);
+ done();
+ });
+ });
+
+ it('should clear the timeout given an id.', function(done) {
+ var count = 0;
+
+ wait('1s', function() {
+ count = 1;
+ }, 'cool');
+
+ wait(500, function() {
+ clear('cool');
+ });
+
+ wait('2s', function() {
+ count.should.equal(0);
+ done();
+ });
+ });
+
+ it('should clear the interval.', function(done) {
+ var count = 0,
+ interval = 0;
+
+ interval = repeat(100, function() {
+ count += 1;
+ });
+
+ wait(600, function() {
+ clear(interval);
+ });
+
+ wait('1s', function() {
+ count.should.equal(5);
+ done();
+ });
+ });
+
+ it('should clear the interval given an id.', function(done) {
+ var count = 0;
+
+ repeat(100, function() {
+ count += 1;
+ }, 'awesome-id');
+
+ wait(400, function() {
+ clear('awesome-id');
+ });
+
+ wait('1s', function() {
+ count.should.equal(3);
+ done();
+ });
+ });
+
+ it('should clear the interval given an id and passing callBefore.', function(done) {
+ var count = 0;
+
+ repeat(100, function() {
+ count += 1;
+ }, 'awesome-id', true);
+
+ wait(400, function() {
+ clear('awesome-id');
+ });
+
+ wait('1s', function() {
+ count.should.equal(4);
+ done();
+ });
+ });
+
+ it('should clear the interval passing callBefore.', function(done) {
+ var count = 0,
+ interval = 0;
+
+ interval = repeat(100, function() {
+ count += 1;
+ }, true);
+
+ wait(400, function() {
+ clear(interval);
+ });
+
+ wait('1s', function() {
+ count.should.equal(4);
+ done();
+ });
+ });
+
+ it('should clear the interval (using until() method).', function(done) {
+ var count = 0,
+ interval = 0,
+ condition = function() {
+ return count > 5;
+ };
+
+ repeat(100, function() {
+ count += 1;
+ });
+
+ interval = until(condition, function() {
+ count = 1;
+ });
+
+ wait(400, function() {
+ clear(interval);
+ });
+
+ wait('1s', function() {
+ count.should.equal(9);
+ done();
+ });
+ });
+
+ it('should clear the interval given an id (using until() method).', function(done) {
+ var str = 'elving',
+ condition = function() {
+ return str === 'osom';
+ };
+
+ wait(500, function() {
+ str = 'osom';
+ });
+
+ until(condition, function() {
+ str = 'Elving';
+ }, 'hip');
+
+ wait(300, function() {
+ clear('hip');
+ });
+
+ wait('1s', function() {
+ str.should.equal('osom');
+ done();
+ });
+ });
+
+ it('should clear the interval given an id and passing a time (using until() method).', function(done) {
+ var count = 0,
+ condition = function() {
+ return count > 5;
+ };
+
+ until(condition, function() {
+ count = 1;
+ }, '1s', 'hip');
+
+ repeat('1s', function() {
+ count += 1;
+ });
+
+ wait('4s', function() {
+ clear('hip');
+ });
+
+ wait('5s', function() {
+ count.should.equal(4);
+ done();
+ });
+ });
+
+ it('should clear all intervals and timeouts in array.', function(done) {
+ var count = 0,
+ timeout = 0;
+
+ wait(100, function() {
+ count = 1;
+ }, 'id1');
+
+ timeout = wait(300, function() {
+ count = 3;
+ });
+
+ repeat('1s', function() {
+ count = 5;
+ }, 'id2');
+
+ clear([timeout, 'id1', 'id2']);
+
+ wait('3s', function() {
+ count.should.equal(0);
+ done();
+ });
+ });
+
+ it('should clear all intervals and timeouts.', function(done) {
+ var count = 0;
+
+ wait(100, function() {
+ count = 1;
+ }, 'id1');
+
+ repeat('1s', function() {
+ count = 5;
+ }, 'id2');
+
+ clear();
+
+ wait('2s', function() {
+ count.should.equal(0);
+ done();
+ });
+ });
+});
Oops, something went wrong.

0 comments on commit d0035b3

Please sign in to comment.