Created
January 21, 2011 12:47
-
-
Save phiggins42/789630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function throttled(t){ | |
var d = new doh.Deferred(); | |
var x = 0; | |
var fn = dojo.throttle(function(a){ | |
x = a; | |
}, 100); | |
fn(1); fn(2); fn(3); fn(4); | |
setTimeout(function(){ | |
t.is(x, 1, "first one won"); | |
d.callback(true); | |
}, 300); | |
return d; | |
}, | |
function throttledTime(t){ | |
var d = new doh.Deferred(); | |
var x = 0, | |
fn = dojo.throttle(function(a){ | |
x = a; | |
}, 500) | |
; | |
fn(10); fn(100); | |
t.is(x, 10, "run once"); | |
setTimeout(function(){ | |
t.is(x, 10, "check after 600ms, 2nd never ran"); | |
d.callback(); | |
}, 600); | |
return d; | |
}, | |
function debounced(t){ | |
var d = new doh.Deferred(); | |
var x = 0; | |
var fn = dojo.debounce(function(a){ | |
x = a; | |
}, 100); | |
fn(1); fn(2); fn(3); fn(4); | |
setTimeout(function(){ | |
t.is(x, 4, "last one won"); | |
d.callback(true); | |
}, 300); | |
return d; | |
}, | |
function debouncedTime(t){ | |
var d = new doh.Deferred(); | |
var x = 0, | |
fn = dojo.debounce(function(a){ | |
x = a; | |
}, 500) | |
; | |
fn(10); fn(100); | |
t.is(x, 0, "hasn't actually run yet"); | |
setTimeout(function(){ | |
t.is(x, 100, "check after 600ms, 2nd exec ran"); | |
d.callback(); | |
}, 600); | |
return d; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d.throttle = function(cb, wait, thisObj){ | |
// summary: | |
// Create a function that will only execute once per `wait` periods. | |
// description: | |
// Create a function that will only execute once per `wait` periods | |
// from last execution when called repeatedly. Useful for preventing excessive | |
// calculations in rapidly firing events, such as window.resize, node.mousemove | |
// and so on. | |
// cb: Function | |
// The callback to fire. | |
// wait: Integer | |
// time to delay before allowing cb to call again. | |
// thisObj: Object? | |
// Optional execution context | |
var canrun = true; | |
return function(){ | |
if(!canrun) return; | |
canrun = false; | |
cb.apply(thisObj || cb, arguments); | |
setTimeout(function(){ | |
canrun = true; | |
}, wait); | |
} | |
} | |
d.debounce = function(cb, wait, thisObj){ | |
// summary: | |
// Create a function that will only execute after `wait` milliseconds | |
// description: | |
// Create a function that will only execute after `wait` milliseconds | |
// of repeated execution. Useful for delaying some event action slightly to allow | |
// for rapidly-firing events such as window.resize, node.mousemove and so on. | |
// cb: Function | |
// A callback to fire. Like hitch() and partial(), arguments passed to the | |
// returned function curry along to the original callback. | |
// wait: Integer | |
// Time to spend caching executions before actually executing. | |
var timer; | |
return function(){ | |
if(timer) clearTimeout(timer); | |
var a = arguments; | |
timer = setTimeout(function(){ | |
cb.apply(thisObj || cb, a); | |
}, wait); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment