var flashStep = 1;
function flash() {
if(flashStep==1) {
document.bgColor="FFFF00";
flashStep=2;
}
else {
document.bgColor="FF0000";
flashStep=1;
}
}
var task = window.setInterval("flash()",1000);
This code is supposed to make the screen flash, but does not work at all. http://jsfiddle.net/phjnM/2/. Why does this not work, it looks flawless to me
The problem is the (global) scope being applied when you pass a string to
setInterval. If you pass a function reference instead it works:var task = window.setInterval(flash,1000);
See http://jsfiddle.net/phjnM/7/
The other option is in jsfiddle choose the “nowrap” option so that your code isn’t defined in an onload/onready handler’s scope…