Results 1 to 5 of 5

Thread: [RESOLVED] Calling functions from functions

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Resolved [RESOLVED] Calling functions from functions

    Is there a way to call another function (e.g function2) from a function (e.g function1), but have the rest of the code in (function1) wait to be executed until the (function2) is finished being executed.

    Thanks in advance,

    Chris1990
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Calling functions from functions

    Javascript is synchronous; unless you're doing something with AJAX, timers, or callbacks, then this is the normal state of the language.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Calling functions from functions

    Thanks for your reply, below is my javascript code,

    In order to calculate the fare you need to know the distance first. So when you call the 'calcFare' function, it first calls the 'calcRoute' function, which calculates and stores the distance in a hidden input field ('txtDistance'), then the 'calcFare' function reads this and calculates the fare.

    I currently have a button for both functions, but i am trying to delete the 'calcRoute' button, and instead call it from the 'calcFare' function. When i use the 'calcFare' button it doesn't seem to call the 'calcRoute' function, and only works if i press the 'calcFare' button twice or press the 'calcRoute' button first.

    i think its because its awaiting feedback from the google maps api, and it is executing the rest of the code before getting the feedback.

    Code:
    <script type="text/javascript">
      var directionDisplay;
      var directionsService = new google.maps.DirectionsService();
      var Gdistance;
      var map;
    
      function initialize() {
        directionsDisplay = new google.maps.DirectionsRenderer();
        var Office = new google.maps.LatLng(53.593338, -2.222371);//41.850033:-87.6500523
        var myOptions = {
          zoom:13,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center: Office
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        directionsDisplay.setMap(map);
      }
      
      function calcRoute() {
        var start = document.getElementById("start").value;
        var end = document.getElementById("end").value;
        var request = {
            origin:start, 
            destination:end,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function(response, status) {
        //  if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
    		Gdistance = response.routes[0].legs[0].distance.value;
    		Gdistance = Gdistance * 0.000621371192;
    		var roundedNumber = roundNumber(Gdistance,2);
    		document.getElementById('txtDistance').value = roundedNumber;
          	    });
      }
    // Round Up Number to 1/2 Descimals
      function roundNumber(num, dec) {
    	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    	return result;
    	}
    
    // Calculate Fare
      function calcFare() {
      //calculate route first
      calcRoute()
      // variables
    	var fareResult
    	var firstMile = 2.20;
    	var pricepMile = 1.80;
    	var distance = document.getElementById('txtDistance').value;
    
      // calculator for Car
      		//exc 1st mile and calc rest of miles by PPM
      			fareResult = (distance - 1) * pricepMile
      		//inc first mile
      			fareResult = fareResult + firstMile		
      // show fare result
    	var roundedNumber = roundNumber(fareResult,2);
    		if (roundedNumber < 1.675) {roundedNumber = "1.70"};	
    	document.getElementById('txtFare').value = roundedNumber;
    	calcType();
    	}
    	
    	function calcType() {
        var VehType = document.getElementById("sVehicle").value;
    	var EstFare = document.getElementById("txtFare").value;
    	var FinalFare = ""
    	
        	if (VehType == "car")
        		{FinalFare = EstFare}
        	if (VehType == "estate")
        	   	{FinalFare = EstFare * 1.25 };
        	if (VehType == "bus")
           		{FinalFare = EstFare * 1.50};
        	if (VehType == "exec")
    	       	{FinalFare = EstFare * 1.75};
    	       	
    	var roundedNumber = roundNumber(FinalFare,2); 
        document.getElementById("txtTypeFare").value = roundedNumber
    	}
    			
    </script>
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Calling functions from functions

    When an AJAX request (which is being made in your calcRoute() function) is made, you set up a callback to be executed when you receive a response. So the callback is a good place to put any functionality that should only execute after the (successful) request.

    So, call your calcRoute() function from a button, and then call calcFare() from the callback in calcRoute(). This is a simplified example:
    Code:
      function calcRoute() {
        var start = document.getElementById("start").value;
        var end = document.getElementById("end").value;
        var request = {
            origin:start, 
            destination:end,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function(response, status) {
        //  if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
    		Gdistance = response.routes[0].legs[0].distance.value;
    		Gdistance = Gdistance * 0.000621371192;
    		var roundedNumber = roundNumber(Gdistance,2);
    		document.getElementById('txtDistance').value = roundedNumber;
                   calcFare();
          	    });
      }
    
    ...
    
    // Calculate Fare
      function calcFare() {
      //calculate route first
      //calcRoute()
    
    ...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Calling functions from functions

    thanks,

    Chris1990
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width