zoukankan      html  css  js  c++  java
  • Jquery的回调函数的使用

    用$.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

  • 相关阅读:
    UVA 10066 The Twin Towers
    UVA 10192 Vacation
    hdu 5018 Revenge of Fibonacci
    hdu 5108 Alexandra and Prime Numbers
    UVA 10252
    UVA 10405最长公共子序列
    数塔
    hdu 2602
    面向对象(五)
    面向对象(三)
  • 原文地址:https://www.cnblogs.com/hubing/p/4885035.html
Copyright © 2011-2022 走看看