首页 > Web开发 > 详细

Jquery的回调函数的使用

时间:2015-10-16 14:49:33      阅读:260      评论:0      收藏:0      [点我收藏+]

用$.Callbacks实现观察者模式

// 观察者模式
var observer = {
    hash: {},
    subscribe: function(id, callback) {
        if (typeof id !== ‘string‘) {
            return
        }
        if (!this.hash[id]) {
            this.hash[id] = $.Callbacks()
            this.hash[id].add(callback)
        } else {
            this.hash[id].add(callback)
        }
    },
    publish: function(id) {
        if (!this.hash[id]) {
            return
        }
        this.hash[id].fire(id)
    }
}
 
// 订阅
observer.subscribe(‘mailArrived‘, function() {
    alert(‘来信了‘)
})
observer.subscribe(‘mailArrived‘, function() {
    alert(‘又来信了‘)
})
observer.subscribe(‘mailSend‘, function() {
    alert(‘发信成功‘)
})
 
// 发布
setTimeout(function() {
    observer.publish(‘mailArrived‘)
}, 5000)
setTimeout(function() {
    observer.publish(‘mailSend‘)
}, 10000)

转:http://snandy.iteye.com/blog/1921793

 

Jquery的回调函数的使用

原文:http://www.cnblogs.com/hubing/p/4885035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!