没有更多的信息,您无法确定您的问题是什么。
但是,简要查看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