This is probably a noob question. The following code works just like I want it but I don't know why.
var x = 0
init_page = function(){
x++;
var y = x;
setTimeout(go, 1000);
function go(){
$('body').append('<div>Y: '+y+'</div>');
}
}
init_page();
If you click the link fast a few times it will print 1,2,3,4...
My issue is that they all call the same function but it acts as if the function has been instanced like this:
function go1(){
$('body').append('<div>Y: 1</div>');
}
function go2(){
$('body').append('<div>Y: 2</div>');
}
function go3(){
$('body').append('<div>Y: 3</div>');
}
Shouldn't they all print the same number at the time of execution (the largest number) instead of the number at the time of clicking? I'd expect to see 4,4,4,4 because go() is the same function for all intervals.