I have the following code;
http://pastebin.com/Wf598D2E
But I can’t get distance value to #result at first click. I have to click twice to see distance in #result. What’s the problem with this code?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The reason is that the call to the google api is asynchronous and your code executes before the call returns populating the ‘dist’ value i.e.
calcRoute takes 500ms to return
by which time
your doc.ready method after that call has already executed.
The reason you see the value the second time is that the value you see is the result of the first calcRoute call
TO FIX:
simple one line amendment