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(){
... 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, data: "TimeStamp=" + TimeStamp, success: function(data){ var json = eval('(' + data + ')'); $('#TextHistory :last-child').after('<p>' + json['msg'] + '</p>'); TimeStamp = json['timestamp']; setTimeout( 'waitForMsg()', 1000 ); }, error: function(XMLHttpRequest, textStatus, errorThrown){ $('#TextHistory :last-child').after('<p>' + errorThrown + '</p>'); setTimeout( 'waitForMsg()', "15000"); }, }); };
As always, any help is appreciated.
Dan
javascript variables scope jquery
Dan twining
source share