Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7734105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:10:00+00:00 2026-06-01T07:10:00+00:00

I have the following code; http://pastebin.com/Wf598D2E But I can’t get distance value to #result

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:10:01+00:00Added an answer on June 1, 2026 at 7:10 am

    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

        $(document).ready(function(){
                $("#send").click(function(event){
                        calcRoute();
                });
    
        });
    
        function calcRoute() {
          var start = document.getElementById("from").value;
          var end = document.getElementById("to").value;
          var ret = 0;
          var request = {
                origin:start,
                destination:end,
                travelMode: google.maps.TravelMode.DRIVING
          };
          directionsService.route(request, function(result, status) {
                //this is the onComplete method so do your UI amendments from in here
                if (status == google.maps.DirectionsStatus.OK) {
                  directionsDisplay.setDirections(result);
                  dist = result.routes[0].legs[0].distance.value;
                  $("#result").html(dist);
                }else{
                        alert("Error");
                }
          });  
    
        }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following simple grid960 HTML code: http://pastebin.com/m1SYMH5S but it looks verry different
I have the following code: http://pastebin.com/U9qYSmET When I try to telnet into my server
i have the following string: http://pastebin.com/d29ae565b i need to separate each value and put
I have the following code: http://pastebin.com/gg4kgGKz On line 240 of the pastebin code, I
I have the following sample HTML code: http://pastebin.com/Mya6tPc6 And here is the jQuery code
I have the following code: http://www.nomorepasting.com/getpaste.php?pasteid=22615 Which is called by the javascript mentioned in
I have to following code: http://www.nomorepasting.com/getpaste.php?pasteid=22987 If PHPSESSID is not already in the table
I have the following code: http://jsfiddle.net/uRCL2/1 At the moment the result is shown in
I have the following code: $file = 'http://3.bp.blogspot.com/-AGI4aY2SFaE/Tg8yoG3ijTI/AAAAAAAAA5k/nJB-mDhc8Ds/s400/rizal001.jpg'; $newfile = '/img/submitted/yoyo.jpg'; if ( copy($file,
I have the following code to open google maps: NSString *urlString = [NSString stringWithFormat:@http://maps.google.com/maps?q=%@,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.