javascriptgoogle-chromegoogle-chrome-extensioniconsanimated

Make an animated Chrome extension icon?


I am trying to make a new Chrome extension, and I need the icon to automatically change like a gif with an array of images, and repeat forever. My problem is I can't get the Javascript loop to work. Here is what I've got:

var min = 1;
var max = 12;
var current = min;

  if (current > max)
    current = min;
}

var keep_switching_icon = true;
function rotateIcon()
{               
   if ( keep_switching_icon )
   {
      chrome.browserAction.setIcon({path:"icon" + current + ".png"});
      current++;
      window.setTimeout(rotateIcon, 300);
   }
}

Solution

  • A few changes should be done:

    1. Checking if current > max should be moved inside the function

    console.clear();
    
    var min = 1;
    var max = 12;
    var current = min;
    
    var keep_switching_icon = true;
    
    function rotateIcon() {
      if (keep_switching_icon) {
        //chrome.browserAction.setIcon({path:"icon" + current + ".png"});
        console.log(current);
        if (current++ > max) {
          current = min;
        };
    
        window.setTimeout(rotateIcon, 300);
      }
    }
    
    rotateIcon();

    Just instead of console.log(current), uncomment your chrome.browserAction function call and delete the first line.