0

I tried making a Chrome progress Rich Notification but the status bar won't move.

I would think this code would work. The status bar will go up by 1% every 40ms. The notification disappears after 4 seconds (happens to be 100% too). I think there is something wrong with my 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);
  });
};  
johnmayer
  • 33
  • 4
  • 1
    What do you do with `k`? You're calling `setInterval` properly, but other than changing the value of `k`, you're not actually doing anything with it that I can see. – Kevin Jul 27 '17 at 18:37

1 Answers1

3

Currently you're assigning progress to whatever value setInterval returns just once.

You need to update the notification each 40ms with the new progress value using chrome.notifications.update:

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);
  });
};
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Doesn't work for me on chrome 78, it updates only once, from 0% to 1% – Codey Nov 06 '19 at 08:29
  • @Codey, it works. The problem you see is likely due to unloading of the event page, which can be prevented as I've shown [here](/a/58577052). I [really tried to help you](/q/58604309/) multiple times but I can't do it remotely. – wOxxOm Nov 06 '19 at 08:53
  • Can you please upload a minimal github repo so I can download, as answer to my post? I will vote & accept for a working solution. @wOxxOm Thanks for your time – Codey Nov 06 '19 at 09:49
  • Here it is https://puu.sh/EBo5G/cf75e40413.zip I've simply copypasted the code I've already shown. – wOxxOm Nov 06 '19 at 09:59
  • @wOxxOm I added the extension as is, it shows 1% for a few seconds and then disappears, have you checked it on chorome 78? – Codey Nov 06 '19 at 10:57
  • It works in 78 and 80. Like I said, evidently I can't help you. Sounds like there's a bug in your OS or there's something wrong with your installation of Chrome. – wOxxOm Nov 06 '19 at 10:59