When you do:
setTimeout(switchBG(index), 10000);
you are calling switchBG(index)
immediately, and then passing it's return value (which is undefined
) to setTimeout
. Instead you want to pass a function reference, and then pass the additional arguments to setTimeout
:
setTimeout(switchBG, 10000, index);
If you want to use additional arguments to setTimeout
like that to work in Internet Explorer, you'll need to shim it. It will work in every other browser without a shim.
If you want to support IE and don't want to use the shim, you can create an extra anonymous function to achieve the same result:
setTimeout(function(){ switchBG(index); }, 10000);