javascriptgoogle-chromegoogle-chrome-extensionsetintervalrich-notifications

Chrome Progress Rich Notification Status won't Move Up


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);
  });
};  

Solution

  • 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);
      });
    };