won*_*nea 9 datejs web-notifications typescript
如何使用设置超时创建TypeScript Web通知?
我创建了以下代码,但在默认超时之前关闭通知不起作用:
export function createNotification(title: string, body: string) {
let now: Date = new Date(Date.now());
let date = now.add(5).seconds();
let options = {
body: body,
requireInteraction: false,
timestamp: date
};
new Notification(title, options);
}
Run Code Online (Sandbox Code Playgroud)
作为测试,我想在TypeScript中创建一个持续五秒钟的通知,覆盖浏览器的默认行为.我看不到TypeScript打字文件中列出的任何其他选项对我有帮助:
我正在使用Chrome开发和测试这个软件,虽然我使用Firefox作为我的主浏览器.因此,任何在Chrome中正常运行但不包含黑客或浏览器特定代码的解决方案都可以.
你尝试过Notification.close()吗?
function spawnNotification(theBody,theIcon,theTitle) {
var options = {
body: theBody,
icon: theIcon
}
var n = new Notification(theTitle,options);
setTimeout(n.close.bind(n), 4000);
}
Run Code Online (Sandbox Code Playgroud)
来源:https://developer.mozilla.org/en-US/docs/Web/API/Notification/close
| 归档时间: |
|
| 查看次数: |
861 次 |
| 最近记录: |