Created
September 5, 2011 19:11
-
-
Save soney/1195701 to your computer and use it in GitHub Desktop.
Returns a function that when called, doesn't execute immediately, but waits update_interval ms before executing. If called with different arguments before then, it will
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
var get_updater = function(do_update, update_interval) { | |
update_interval = update_interval || 20; | |
var calls = []; | |
var queue_update = function() { | |
var update_index = undefined; | |
for(var i = 0, len = calls.length; i<len; i++) { | |
if(calls[i].self === this) { | |
update_index = i; | |
break; | |
} | |
} | |
if(update_index === undefined) { | |
var call = { | |
self: this | |
, args: arguments | |
}; | |
call.timeout_id = window.setTimeout(function() { | |
do_update.apply(call.self, call.args); | |
for(var i = 0, len = calls.length; i<len; i++) { | |
if(call[i] === call) { | |
calls.splice(i, 1); | |
break; | |
} | |
} | |
}, update_interval); | |
} else { | |
calls[update_index].args = arguments; | |
} | |
}; | |
var update = function() { | |
queue_update.apply(this, arguments); | |
}; | |
return update; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment