【问题标题】:Chrome Progress Rich Notification Status won't Move UpChrome Progress Rich Notification 状态不会上移
【发布时间】:2018-01-03 15:27:07
【问题描述】:

我尝试制作 Chrome 进度丰富通知,但状态栏不会移动。

我认为这段代码会起作用。状态栏将每 40 毫秒上升 1%。通知会在 4 秒后消失(也可能是 100%)。我觉得我的setInterval有问题

var notifyStatus = function(title, message) {
  var k = 0;
  chrome.notifications.create('', {
    'type':    'progress',
    'iconUrl': 'images/icon128.png',
    'title':   title,
    'message': message || '',
    'progress': setInterval(function() {
        if (k>100) {k;}
        else {k++;}
    },40)
  }, function(nid) {
    // Automatically close the notification in 4 seconds.
    window.setTimeout(function() {
      chrome.notifications.clear(nid);
    }, 4000);
  });
};  

【问题讨论】:

  • 你用k做什么?您正确地调用了setInterval,但除了更改k 的值之外,您实际上并没有对它做任何我能看到的事情。

标签: javascript google-chrome google-chrome-extension setinterval rich-notifications


【解决方案1】:

目前,您将 progress 分配给 setInterval 返回的任何值仅一次

您需要使用 chrome.notifications.update 每 40 毫秒使用新的进度值更新通知:

var notifyStatus = function(title, message, timeout) {
  chrome.notifications.create({
    type: 'progress',
    iconUrl: 'images/icon128.png',
    title: title,
    message: message || '',
    progress: 0
  }, function(id) {
    // Automatically close the notification in 4 seconds by default
    var progress = 0;
    var interval = setInterval(function() {
      if (++progress <= 100) {
        chrome.notifications.update(id, {progress: progress}, function(updated) {
          if (!updated) {
            // the notification was closed
            clearInterval(interval);
          }
        });
      } else {
        chrome.notifications.clear(id);
        clearInterval(interval);
      }
    }, (timeout || 4000) / 100);
  });
};

【讨论】:

  • 在 chrome 78 上对我不起作用,它只更新一次,从 0% 到 1%
  • @Codey,它有效。您看到的问题可能是由于事件页面的卸载造成的,正如我在here 中显示的那样,可以防止这种情况发生。我really tried to help you 多次,但我无法远程操作。
  • 您能否上传一个最小的 github 存储库,以便我可以下载,作为我帖子的答案?我会投票并接受一个可行的解决方案。 @wOxxOm 感谢您的宝贵时间
  • 这里是puu.sh/EBo5G/cf75e40413.zip 我只是复制粘贴了我已经显示的代码。
  • @wOxxOm 我按原样添加了扩展程序,它显示 1% 几秒钟然后消失,你在 chorome 78 上检查了吗?
猜你喜欢
  • 2021-02-10
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多