日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html 订阅发布,发布-订阅模式.html

發布時間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html 订阅发布,发布-订阅模式.html 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Title

var pubsub = {};

(function(myObject) {

// Storage for topics that can be broadcast 可以廣播的主題的存儲

// or listened to 或者聽

var topics = {};

// A topic identifier 主題標識符

var subUid = -1;

// Publish or broadcast events of interest 發布或廣播感興趣的事件

// with a specific topic name and arguments 具有特定的主題名稱和參數

// such as the data to pass along 如數據傳遞

myObject.publish = function( topic, args ) {

if ( !topics[topic] ) {

return false;

}

var subscribers = topics[topic],

len = subscribers ? subscribers.length : 0;

while (len--) {

subscribers[len].func( topic, args );

}

return this;

};

// Subscribe to events of interest 訂閱感興趣的事件

// with a specific topic name and a 具有特定的主題名稱和

// callback function, to be executed 回調函數,將被執行

// when the topic/event is observed 當觀察到主題/事件時

myObject.subscribe = function( topic, func ) {

if (!topics[topic]) {

topics[topic] = [];

}

var token = ( ++subUid ).toString();

topics[topic].push({

token: token,

func: func

});

return token;

};

// Unsubscribe from a specific 取消訂閱

// topic, based on a tokenized reference 主題,基于標記化參考

// to the subscription 訂閱

myObject.unsubscribe = function( token ) {

for ( var m in topics ) {

if ( topics[m] ) {

for ( var i = 0, j = topics[m].length; i < j; i++ ) {

if ( topics[m][i].token === token ) {

topics[m].splice( i, 1 );

return token;

}

}

}

}

return this;

};

}( pubsub ));

// A simple message logger that logs any topics and data received through our 一個簡單的消息記錄器,記錄通過我們接收的任何主題和數據。

// subscriber 用戶

var messageLogger = function ( topics, data ) {

console.log( "Logging: " + topics + ": " + data );

};

// Subscribers listen for topics they have subscribed to and 訂閱者監聽他們訂閱的主題和

// invoke a callback function (e.g messageLogger) once a new 調用一個新的回調函數(例如MasigelgGER)

// notification is broadcast on that topic 這個主題廣播通知

var subscription = pubsub.subscribe( "inbox/newMessage", messageLogger );

// Publishers are in charge of publishing topics or notifications of 出版商負責出版主題或通知。

// interest to the application. e.g: 對應用程序有興趣。例如:

pubsub.publish( "inbox/newMessage", "hello world!" );

// or

pubsub.publish( "inbox/newMessage", ["test", "a", "b", "c"] );

// or

pubsub.publish( "inbox/newMessage", {

sender: "hello@google.com",

body: "Hey again!"

});

// We can also unsubscribe if we no longer wish for our subscribers 如果我們不再希望我們的訂戶,我們也可以退訂。

// to be notified 被通知

pubsub.unsubscribe( subscription );

// Once unsubscribed, this for example won't result in our 一旦退訂,這將不會導致我們的

// messageLogger being executed as the subscriber is MasaGelgGER作為訂閱服務器執行

// no longer listening 不再聽

pubsub.publish( "inbox/newMessage", "Hello! are you still there?" );

一鍵復制

編輯

Web IDE

原始數據

按行查看

歷史

總結

以上是生活随笔為你收集整理的html 订阅发布,发布-订阅模式.html的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。