0

This is looping in a timeout function. The nw remains undefined or is reset to undefined again at each new start. Why is that?

$("#wert"+i).load('variable.html #w'+i);
if(! nw){
alert(nw);
var nw = eval('neuerwert'+i); // Here I set the var nw, so why is it undefined again the next time around?
}
if ($("#w"+i).html() != nw){
wertaenderung('#wert'+i);
nw = $("#w"+i).html();
};
Coffeehouse
  • 505
  • 1
  • 3
  • 15

3 Answers3

1

the variable nw has to be in the right scope:

var nw;
$("#wert"+i).load('variable.html #w'+i);
if(! nw){
    alert(nw);
    nw = eval('neuerwert'+i);
}
if ($("#w"+i).html() != nw){
    wertaenderung('#wert'+i);
    nw = $("#w"+i).html();
};

you USED the variable before you DECLARED it

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
1

try moving nw out of load function:

var nw;
$("#wert" + i).load('variable.html #w' + i);
if (!nw) {
    alert(nw);
    nw = eval('neuerwert' + i);
}
if ($("#w" + i).html() != nw) {
    wertaenderung('#wert' + i);
    nw = $("#w" + i).html();
};
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
1

Just remove the "var" in this line :

var nw = eval('neuerwert'+i);

So you will initialize the nw variable in the global context.

By writing var nw = ... you create a local variable that is removed when you leave the callback function.

Damien
  • 8,889
  • 3
  • 32
  • 40