Skip to content

Commit

Permalink
Adding Function#periodical
Browse files Browse the repository at this point in the history
  • Loading branch information
petebrowne committed Nov 30, 2010
1 parent 92a4307 commit 869586c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
17 changes: 16 additions & 1 deletion dist/classified.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,26 @@ classify(Function, function() {
return this.delay.apply(this, [ 0.01 ].concat(slice.call(arguments)));
});

// Schedules the function to run in the specified intervals of time, passing
// any arguments given.
//
// Behaves much like `window.setInterval`, but the interval is in seconds
// rather than milliseconds. Returns an integer ID that can be used to
// clear the interval with `window.clearInterval` before it runs.
def('periodical', function(interval) {
var method = this,
args = slice.call(arguments, 1);

return this.__intervalID__ = setInterval(function() {
return method.apply(null, args);
}, interval * 1000);
});

// If a #delay, #defer, or #periodical call has been made, calling #stop
// will stop the function call from being made.
def('stop', function() {
clearTimeout(this.__timeoutID__);
// clearInterval(this.__intervalID__);
clearInterval(this.__intervalID__);
});
});

Expand Down
2 changes: 1 addition & 1 deletion dist/classified.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions spec/classified/functionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,40 @@ describe('Function', function() {
});
});

describe('#periodical', function() {
it('runs a function periodically', function() {
var count = 0;
var method = function() {
count++;
};

var id = method.periodical(0.05);
expect(count).toBe(0);

waits(150);
runs(function() {
clearInterval(id);
expect(count).toBe(2);
});
});

it('passes arguments to the deferred function', function() {
var args;
var method = function() {
args = Array.prototype.slice.call(arguments);
};

var id = method.periodical(0.05, 1, 2, 3);
expect(args).toBeUndefined();

waits(100);
runs(function() {
clearInterval(id);
expect(args).toEqual([ 1, 2, 3 ]);
});
});
});

describe('#stop', function() {
it('stops delayed calls', function() {
var delayed = false;
Expand Down Expand Up @@ -126,5 +160,22 @@ describe('Function', function() {
expect(defered).toBe(false);
});
});

it('stops periodical calls', function() {
var count = 0;
var method = function() {
count++;
};

method.periodical(0.05);
waits(125);
runs(function() {
method.stop();
})
waits(50);
runs(function() {
expect(count).toBe(2);
});
});
});
});
17 changes: 16 additions & 1 deletion src/extensions/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,25 @@ classify(Function, function() {
return this.delay.apply(this, [ 0.01 ].concat(slice.call(arguments)));
});

// Schedules the function to run in the specified intervals of time, passing
// any arguments given.
//
// Behaves much like `window.setInterval`, but the interval is in seconds
// rather than milliseconds. Returns an integer ID that can be used to
// clear the interval with `window.clearInterval` before it runs.
def('periodical', function(interval) {
var method = this,
args = slice.call(arguments, 1);

return this.__intervalID__ = setInterval(function() {
return method.apply(null, args);
}, interval * 1000);
});

// If a #delay, #defer, or #periodical call has been made, calling #stop
// will stop the function call from being made.
def('stop', function() {
clearTimeout(this.__timeoutID__);
// clearInterval(this.__intervalID__);
clearInterval(this.__intervalID__);
});
});

0 comments on commit 869586c

Please sign in to comment.