Javascript scope

I am having problems with a global javascript variable (called TimeStamp) that is not defined by onload ... at least I think the problem is.

I start by defining a TimeStamp.

$(document).ready(function(){ // AddTest(); var TimeStamp = null; waitForMsg(); }); 

... waitForMsg starts using TimeStamp and updates it when the ajax call succeeds. At least this idea, but at the moment nothing starts because "TimeStamp is not defined" ... although I defined it before! (Urgh).

If I override Timestamp in waitForMsg, it just gets reset instead of using the updated value from the successful ajax function.

  function waitForMsg(){ $.ajax({ type: "POST", url: "backend.php", async: true, cache: false, timeout:50000, /* Timeout in ms */ data: "TimeStamp=" + TimeStamp, success: function(data){ var json = eval('(' + data + ')'); $('#TextHistory :last-child').after('<p>' + json['msg'] + '</p>'); TimeStamp = json['timestamp']; setTimeout( 'waitForMsg()', /* Request next message */ 1000 /* ..after 1 seconds */ ); }, error: function(XMLHttpRequest, textStatus, errorThrown){ $('#TextHistory :last-child').after('<p>' + errorThrown + '</p>'); setTimeout( 'waitForMsg()', /* Try again after.. */ "15000"); /* milliseconds (15seconds) */ }, }); }; 

As always, any help is appreciated.

Dan

+2
javascript variables scope jquery
source share
3 answers

change

 $(document).ready(function(){ // AddTest(); var TimeStamp = null; waitForMsg(); }); 

to

 var TimeStamp = null; $(document).ready(function(){ // AddTest(); waitForMsg(); }); 

Thus, he will live in a global scope, and not only within the framework of the finished function.

As a further note, you should change the setTimeout statements from the form setTimeout(string to eval,delay) to setTimeout(function reference to run, delay) . So:

 setTimeout(waitForMsg,1000); 

to avoid an unnecessary call to eval .

Next, consider the change

 var json = eval('(' + data + ')'); 

to

 var json = JSON && JSON.parse ? JSON.parse(data) : eval('(' + data + ')'); 

so you can use your own JSON analysis in modern browsers.

Even better, let jQuery deserialize the JSON for you by adding dataType: 'json' as the parameter to call ajax :)

+2
source share

Do not skip lines to be irradiated. It is inefficient, it is difficult to debug and break the scope:

 setTimeout('waitForMsg()',1000); 

Perform the functions:

 setTimeout(waitForMsg,1000); 
0
source share

You must define var TimeStamp outside of any functions in the page area.

When you define a variable in a code block, it will be visible only at the same level, and it will not be visible in the methods that you call from this code block.

When you write the values โ€‹โ€‹of the TimeStamp variable in your ajax call (and without defining it with "var"), it automatically determines or reuses the TimeStamp variable in the page area, however, since you are trying to read TimeStamp (data: "TimeStamp =" + TimeStamp "before you even write to it, then it will generate a" not defined "error.

In any case: consider avoiding the use of page area variables.

0
source share

All Articles