I made a closure function with the help of google. The function works as it should. However, I'm not sure to know how it works.
function ShowQuestion(i) {
console.log(i); // here 1 to 10 are logged immediately.
return function() {
console.log(i); // here the number are only logged when I mouse over on any li.
}
}
setTimeout(function() {
for (i = 0; i < document.getElementById('Previousli').getElementsByTagName('ul')[0].children.length; i++) {
document.getElementById('Previousli').getElementsByTagName('ul')[0].children[i].onmouseover = ShowQuestion(i);
}
}, 10000);
First of all, I'm wondering why the first console.log(i) log 1 to 10 immediately after the 10 seconds time out, but the second console.log(i) log the "index" only when I mouse over a li?