Last active
December 28, 2015 04:19
-
-
Save oxUnd/7441265 to your computer and use it in GitHub Desktop.
js自定义事件
This file contains 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 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