1.创建消息
var ManagedClientStatus = {
historyMessage: [],
curMessage: null
}
ManagedClientStatus.addMessage = function (clientObj, statusCode) {
var curMessage = {
client: clientObj,
statusCode: statusCode,
createAt: new Date()
}
if (ManagedClientStatus.curMessage === null) {
ManagedClientStatus.curMessage = curMessage
} else {
ManagedClientStatus.historyMessage.push(curMessage)
setTimerPush()
}
}
var setTimerPush = function () {
var setTimer1 = setInterval(function () {
if (ManagedClientStatus.curMessage === null) {
if (ManagedClientStatus.historyMessage.length > 0) {
ManagedClientStatus.curMessage = ManagedClientStatus.historyMessage[0]
ManagedClientStatus.historyMessage.splice(0,1)
} else {
clearInterval(setTimer1)
}
}
}, 500)
export default ManagedClientStatus
2.发送消息
(这个模块在vue中使用)
created: function(){
//vue中深度检测变量
watch: {
curClientMessage: {
handler(newName, oldName) {
console.log('obj.a changed');
},
deep: true,
immediate:true
}
}
//js本身的检测对象内部变量的变化
var the = this
Object.defineProperty(ManagedClientStatus, 'curMessage', {
get: function () {
return the.curClientMessage
},
set: function (value) {
the.curClientMessage = value
}
})
3.输出
将this.curClientMessage 的值输出到消息展示模块