I try to change a welcome message based on mouse coords. It has to change after given a number.
There is somewhere a problem, but I cant figure it out!
var str = ["Hello", "Bonjour", "Salut"],
len = str.length,
i = 0;
document.onmousemove = function(event) {
var count = i++,
random = (count) % len;
if (count > 60) {
document.body.innerHTML = str[random];
i = 0;
}
console.log(count);
};
A fiddle for better interpretation.
Any ideas?
Your logic will only ever generate the following calculation:
Or at least it will only ever be output when
i > 60and so your output will always be based on that calculation – not what you are expecting.A version without reusing the previous value: