DelayedFunctionScheduler tick, setTimeout/Interval delay defaults to 0

If ommited or null, delay for refered methods will default to 0. This
will make setTimeout and setInterval methods to behave as expected by
[HTML5 specs](http://www.w3.org/TR/html51/webappapis.html#timers):

"Let timeout [delay] be the second argument to the method, or zero if the
argument was omitted."

This commit also fixes an issue with tick() being called without arguments,
that causes the scheduler to break and stop working after this call.
This commit is contained in:
Caio Cunha
2013-03-13 10:55:46 -03:00
parent 6b2d8da55f
commit e7a930a5b3
2 changed files with 15 additions and 0 deletions

View File

@@ -12,6 +12,19 @@ describe("DelayedFunctionScheduler", function() {
expect(fn).toHaveBeenCalled();
});
it("defaults delay to 0", function() {
var scheduler = new jasmine.DelayedFunctionScheduler(),
fn = jasmine.createSpy('fn');
scheduler.scheduleFunction(fn);
expect(fn).not.toHaveBeenCalled();
scheduler.tick(0);
expect(fn).toHaveBeenCalled();
});
it("optionally passes params to scheduled functions", function() {
var scheduler = new jasmine.DelayedFunctionScheduler(),
fn = jasmine.createSpy('fn');

View File

@@ -5,11 +5,13 @@ jasmine.DelayedFunctionScheduler = function() {
var delayedFnCount = 0;
self.tick = function(millis) {
millis = millis || 0;
runFunctionsWithinRange(currentTime, currentTime + millis);
currentTime = currentTime + millis;
};
self.scheduleFunction = function(funcToCall, millis, params, recurring, timeoutKey, runAtMillis) {
millis = millis || 0;
timeoutKey = timeoutKey || ++delayedFnCount;
runAtMillis = runAtMillis || (currentTime + millis);
scheduledFunctions[timeoutKey] = {