-2

I got this JavaScript code on my webpage:

var dLastUpdate = new Date;

var getData = function () {
   alert(dLastUpdate);
   $.post("mypage.aspx?accion=getData", {}, function (data) {RefreshData(data)})
}

function RefreshData(rValue) {    
   dLastUpdate = rValue.UpdateTime;
}

getData function it's called many times. In theory, dLastUpdate should be empty the first call, and save the value returned from server. Never the less, in the alert(dLastUpdate); the variable already have value on the first call (page load).

How can this be posible?

Could be that the client is caching the value from previous run? Thanks!

ericpap
  • 2,917
  • 5
  • 33
  • 52
  • 1
    Sounds like the everyday ajax problem http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – elclanrs Mar 02 '15 at 21:47
  • 8
    Why wouldn't it have a value, it's set to the current date at instantiation ? – adeneo Mar 02 '15 at 21:47
  • 1
    You set it right here: `var dLastUpdate = new Date;`. So of course it has a value. Presumably you meant to just do `var dLastUpdate;` and leave it undefined? – Matt Burland Mar 02 '15 at 21:47
  • 1
    @elclanrs Actually the problem is entirely independent of the Ajax call. – Paul Roub Mar 02 '15 at 21:48

1 Answers1

4

You initialise it and give it a value on line 1.

var dLastUpdate = new Date;

That doesn't depend on the Ajax call being processed.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335