Why is prevColor always undefined
?
This code should log a new color (actualColor
) and the previous color (prevColor
). However I cannot save into prevColor
from inside the setInterval
function. Where is the bug? I don´t think it is a context problem. But I´m not sure. There´s no this
inside...
Can you tell me how I can save the value of actualColor
in prevColor
from inside the setInterval
function?
var actualColor;
var prevColor;
// do some user action and change actualColor
setInterval(function () {
// only log the color if it differs from prevColor
if (actualColor != prevColor) {
console.log("actualColor: " + actualColor + ", prevColor: " + prevColor);
}
prevColor = actualColor;
}, 100);
Console:
actualColor: acff06, prevColor: undefined