Skip to content

Instantly share code, notes, and snippets.

@oxUnd
Last active December 28, 2015 04:19
Show Gist options
  • Save oxUnd/7441265 to your computer and use it in GitHub Desktop.
Save oxUnd/7441265 to your computer and use it in GitHub Desktop.
js自定义事件
// -------------------- 事件队列 --------------------
var SLICE = [].slice;
var events = {};
function trigger(type /* args... */) {
var list = events[type];
if (!list) {
return;
}
var arg = SLICE.call(arguments, 1);
for(var i = 0, j = list.length; i < j; i++) {
var cb = list[i];
if (cb.f.apply(cb.o, arg) === false) {
break;
}
}
}
function on(type, listener, context) {
var queue = events[type] || (events[type] = []);
queue.push({f: listener, o: context});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment