Neel Bhasin
Neel Bhasin

Reputation: 759

calling a function with setinterval within another function in google maps apv v3

i am writing this code to call a function after some time interval but the function is not preforming it functionality .when i am normally calling the function pus() it is running but with setInterval it is not working.please give me its solution.

 var ltnlg = null;
 var flightPlanCoordinates = [];
 var nl2 = 10.9646;
 var ng2 = 72.8787;
 var lt2, ln2;
 var ltnlg = null;

 function lin() {
           flightPlanCoordinates = [
     new google.maps.LatLng(29.0167, 77.3833),
     new google.maps.LatLng(21.7679, 78.8718),
     new google.maps.LatLng(18.9647, 72.8258)
     ];

          var flightPath = new google.maps.Polyline({
               path: flightPlanCoordinates,
               strokeColor: "#FF0000",
               strokeOpacity: 1.0,
               strokeWeight: 2
               });

           flightPath.setMap(map);
           pl = new google.maps.Polyline();
           //  window.setInterval("pus()", 10000);
          pus();

           }
       }

       function pus() {



           ltnlg = new google.maps.LatLng(nl2, ng2);
           flightPlanCoordinates.push(ltnlg);
           lt2 = nl2;
            n12 = lt2 + 0.0660;
          ln2 = ng2;
          ng2 = ln2 + 0.0660;

       }   

Upvotes: 2

Views: 1103

Answers (2)

Dr.Molle
Dr.Molle

Reputation: 117334

The question here should not be "why this didn't work with setInterval", it should be "why does it work from within the function" , because it shouldn't.

I guess the drawing of the polygon is a asynchronous process, it's not finished when you call pus() from within the function, that's why the changes made on flightPlanCoordinates will still have an effect.

But the correct way to apply the modified path to the polyline is to call in pus() :

flightPath.setPath(flightPlanCoordinates);

(Note: you must make flightPath global to be able to access it from outside of lin())

Upvotes: 1

sbmaxx
sbmaxx

Reputation: 886

Never pass callback as string.

window.setInterval(pus, 10000);

or if you want pass some params

window.setInterval(function() {
    pus(a, b, c);
}, 10000);

Upvotes: 2

Related Questions