I’m trying to build a loading indicator with a image sprite and I came up with this function
function setBgPosition() {
var c = 0;
var numbers = [0, -120, -240, -360, -480, -600, -720];
function run() {
Ext.get('common-spinner').setStyle('background-position', numbers[c++] + 'px 0px');
if (c<numbers.length)
{
setTimeout(run, 200);
}else
{
setBgPosition();
}
}
setTimeout(run, 200);
}
so the out put is looks like this
I had to use setBgPosition(); inside else to keep this running in a loop so now my problem is how to stop this loop once I want [load finished]?
setTimeoutreturns a timer handle, which you can use to stop the timeout withclearTimeout.So for instance:
So you’d use that as:
Note that rather than having
setBgPositioncall itself again, I’ve just had it setcback to0. Otherwise, this wouldn’t work. Also note that I’ve used0as a handle value for when the timeout isn’t pending;0isn’t a valid return value fromsetTimeoutso it makes a handy flag.This is also one of the (few) places I think you’d be better off with
setIntervalrather thansetTimeout.setIntervalrepeats. So:Used like this:
All of the above notwithstanding, I’d want to find a way to make
setBgPositionstop things itself, by detecting that some completion condition has been satisfied.