沒有更多的信息,您無法確定您的問題是什麼。
但是,簡要查看sdk/notifications
documentation和源代碼,指出您正在嘗試使用不存在的方法:notifications.close()
。 sdk/notifications
中沒有這種方法。
您嘗試使用此方法的一個可能原因是您正在使用附加SDK sdk/notifications
將Web Notification API,more detail合併。
附加SDK,sdk/notifications
無法以編程方式關閉代碼中的通知。因此,您無法爲使用此接口的通知設置超時。但是,在某些操作系統/窗口系統中,這些通知已經存在默認超時。
您需要自行顯示面板,或者使用User Notifications and Alerts中描述的鉻界面。
另外,如果您只能致電setTimeout()
,這種情況並不常見。這在大多數情況下都不會被界定。通常,你會需要使用sdk/timers
有:
var { setTimeout } = require("sdk/timers");
在某些情況下,你可能能夠使用window.setTimeout()
,當window
適當定義(你可能要爲自己設定)。
將我的回答中的代碼修改爲Prevent XUL notificationBox from closing when button is hit(如果您需要按鈕,該答案會告訴您如何去做)以及我的其他答案:我相信您期望的東西是(代碼超時在底部):
function showNotificationBox(text) {
//Create some common variables if they do not exist.
if (window === null || typeof window !== "object") {
// Add/remove a "/" to comment/un-comment the code appropriate for your add-on:
//* Add-on SDK:
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/
/* Overlay and bootstrap (from almost any context/scope):
var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
//*/
}
if (typeof gBrowser === "undefined") {
var gBrowser = window.gBrowser;
}
let notifyBox = gBrowser.getNotificationBox();
//appendNotification(label , value , image (URL) , priority , buttons, eventCallback)
let theNotification = notifyBox.appendNotification(text, "Test notification unique ID",
"chrome://browser/content/aboutRobots-icon.png",
notifyBox.PRIORITY_INFO_HIGH, [], null);
//* Add-on SDK:
var { setTimeout } = require("sdk/timers");
setTimeout(theNotification.close(), 10000);
//*/
/* Overlay and bootstrap:
let timerCallback = {
notify:function notify() {theNotification.close(); }
}
let closeNotificationTimer = Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
closeNotificationTimer.initWithCallback(timerCallback,10000,
Components.interfaces.nsITimer.TYPE_ONE_SHOT);
//*/
}
注意:我已將超時值更改爲您的問題代碼中1秒鐘的10秒。一秒鐘是期望顯示您實際上希望用戶看見和理解的任何事情的不合理時間。
以上實現notificationBox中的用戶通知。因此就會出現在Firefox窗口內:
也可以使用nsIAlertsService這是什麼sdk/notifications
用途。這通常會在屏幕右下角顯示一個警告框,可能位於Firefox窗口的外面(例如,參見nsIAlertsService上的圖片)。通知可能會顯示在其他地方,具體取決於您如何設置窗口系統(這取決於操作系統)。但是,the documentation沒有清除通知或設置超時的方法。但是,the interface definition確實表明closeAlert()
方法確實存在。 sdk/notifications
的源代碼不會將其公開給附加SDK。因此,您需要使用chrome界面。我已更新文檔以顯示closeAlert()
。
如(從nsIAlertsService截取和修改一些代碼):
//* Add-on SDK:
var {Cc, Ci} = require("chrome");
//*/
/* Overlay and bootstrap:
const Cc = Components.classes;
const Ci = Components.interfaces;
//*/
function showNotifcation(title, text) {
var alertsService = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
try {
//The second use of title is the alert name.
alertsService.showAlertNotification(icon, title, text, false, "", null, title);
} catch (e) {
// This can fail on Mac OS X
}
//* Add-on SDK:
var { setTimeout } = require("sdk/timers");
setTimeout(alertsService.closeAlert(title), 10000);
//*/
/* Overlay and bootstrap:
let alertTimerCallback = {
notify:function notify() {alertsService.closeAlert(title); }
}
let closeAlertTimer = Cc["@mozilla.org/timer;1"].createInstance(Components.interfaces
.nsITimer);
closeAlertTimer.initWithCallback(alertTimerCallback,10000,Ci.nsITimer.TYPE_ONE_SHOT);
//*/
}
我只用一個自舉/無需重啓Firefox插件測試上面的代碼。因此,附加SDK代碼可能稍微偏離。
「不工作」。對你的問題沒有足夠的解釋。 – theblindprophet
什麼都行不通,你會得到什麼錯誤?請向我們解釋問題,以便我們幫助您解決問題。 – Pilatus
請將你的問題寫在話題上:尋求調試幫助的問題(「**爲什麼不是這個代碼工作?」)必須包括:•期望的行爲,•特定的問題或錯誤*和* •在問題本身**中重現它所需的最短代碼**。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建一個最小,完整和可驗證示例](http://stackoverflow.com/help/mcve),[我可以在這裏詢問什麼主題?](http://stackoverflow.com/help/主題)和[我如何提出一個好問題?](http://stackoverflow.com/help/how-to-ask)。 – Makyen