如何在一段时间内收听来自window.postMessage
的邮件?
我试图在事件监听器的函数中使用setTimeout
,但这不起作用。
我的尝试:
function processMessages(message) {
//setTimeout(function() {
if (message) {
console.log("you have a message");
} else {
console.log("no messages came"); //<=== I can't get here!
}
//},10000);
window.removeEventListener("message", processMessages, false);
}
window.addEventListener("message", processMessages, false);
答案 0 :(得分:0)
重新排列代码,将.removeEventListener()
调用放在setTimeout()
内,该processMessage
调用不需要在function processMessages(message) {
if (message) {
console.log("you have a message");
} else {
console.log("no messages came"); //<=== I can't get here!
}
}
window.addEventListener("message", processMessages, false);
var timeout = setTimeout(function() {
window.removeEventListener("message", processMessages, false);
}, 10000);
函数内
def build_device_list(self):
self.dev_store_len = len(self.device_list_store)
for i, column_title in enumerate(["Device", "Model", "Serial Number","Size"]):
self.renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn(column_title, self.renderer, text=i)
self.device_list_treeview.append_column(column)
column.set_sort_column_id(i)
self.renderer_status = gtk.CellRendererText()
column_status = gtk.TreeViewColumn("Status",self.renderer_status, text=4, foreground=5)
self.device_list_treeview.append_column(column_status)
for x in range(0, self.dev_store_len):
if self.device_list_store[x][4] == "READ-WRITE":
self.device_list_store[x][5] = "Red"
else:
self.device_list_store[x][5] = "Green"
self.scrollable_treelist = gtk.ScrolledWindow()
self.scrollable_treelist.set_vexpand(True)
self.outter_box.pack_start(self.scrollable_treelist,False,True,0)
self.scrollable_treelist.add(self.device_list_treeview)
答案 1 :(得分:0)
你setTimeout事件运行,但你的removeEventListener消息没有延迟。