2

trying to figure out how to do this and I'm not too sure. I'm trying to update local GPS coordinates every 3 seconds using setTimeout and it's not working properly. I'm very new to js so any help is appreciated. This is what I have so far-

In my main JS file, I have this...

var lat_in={};
var long_in={};
var alt_in={};

$.getScript("test_points.js", function(){
    console.log("TP script go");
});

$("#Initiate").click(function() {

  if (coordinate=="GPS")
     {
     console.log("GPS go");
     lat_out = lat_in;
     long_out = long_in;
     alt_out = alt_in;
         console.log(lat_out, long_out, alt_out)
     }

)}

I'm calling the lat/long/alt from another js document (test_points.js), where the setTimeout is...

setTimeout(function(){
    var lat_in=5;
    var long_in=5;
    var alt_in=-5;
}, 3000);

I have a bunch of setTimeouts one after another, and what I'm trying to do is update the variables every 3 seconds and run them through the if statement again.

Am I even close to doing this properly? Thanks in advance

Kyle Foley
  • 190
  • 7
  • Use [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval). `setTimeout` only works once. – Mouser Aug 03 '16 at 13:12
  • You're declaring new local vars inside the function, not accessing the global ones. But doing asynchronous stuff to global state is a confusing debug session waiting to happen. – Dave Newton Aug 03 '16 at 13:16

4 Answers4

2

You have declared those variables with a "var" keyword which makes them local to the function passed to the timeout function. Please remove var and try again.

EDIT : As other have pointed out, you should use interval instead of timeout and once the changes are reflected, then only you should try accessing them.

binariedMe
  • 4,309
  • 1
  • 18
  • 34
2

JavaScript has two kind of timers, one provides a timeout (delayed execution of a piece of code) called window.setTimeout (because setTimeout is a method of the window object) and a timer that runs a piece of code after a certain interval called window.setInterval.

Watch how both work

setTimeout(function(){
document.getElementById("showcase").innerHTML += "setTimeout fired only once after 3 seconds. <br />"}, 3000);

setInterval(function(){
document.getElementById("showcase").innerHTML += "setInterval fired every after 5 seconds. <br />"}, 5000);


//not part of the solution, just to demonstrate.
setInterval(function(){
document.getElementById("timer").textContent = 1 + parseInt(document.getElementById("timer").textContent);
}, 1000)
#showcase {
  padding: 4px;
  border: 1px solid grey;
  background-color: #f3f3f3;
}
<span id="timer">0</span> seconds <br />
<div id="showcase"></div>

To let a timer run multiple times use setInterval.


On your case

setTimeout(function(){
    var lat_in=5;
    var long_in=5;
    var alt_in=-5;
}, 3000);

When you replace setTimeout to setInterval it still won't work. As others commented, you are declaring the lat_in etc. inside the scope of the function. This means that the variables inside that function are only accessibele inside the function bound to the interval. The keyword is var. It tells the parser to "bind" the variables to the scope of the function. Leave the var and your timer function will overwrite the global variables declared outside the function. The sample below should be fine.

setInterval(function(){
    lat_in=5;
    long_in=5;
    alt_in=-5;
}, 3000);

Also when you first declared the variables they are declared as an empty object.

var lat_in={};
var long_in={};
var alt_in={};  

using {} is the same as doing lat_in = new Object(). However in your function you overwrite them with integers. Maybe this isn't meant to be. Check the documentation on your GPS coding to what input it needs.


While we are at it:

you can stop timers too with window.clearTimeout and window.clearInterval. Each referring to its own function of course.

To do this, you need to store the timer into a variable:

timer = window.SetInterval(function(){ ... }, 3000); //always in milliseconds.
clearInterval(timer); // this line will now clear the timer.

Also a smart person could remark: "I can let setTimeout behave like an interval."

window.setTimeout(myTimerFunction, 3000);
function myTimerFunction(){
    //do stuff here
    window.setTimeout(myTimerFunction, 3000);  //call itself again in three seconds.
}
Community
  • 1
  • 1
Mouser
  • 13,132
  • 3
  • 28
  • 54
0

Since setTime out is asynchronous you should not start expressions like lat_out = lat_in; before initialising.

And also you are declaring the variables using Var which makes them local.so remove var.

tsadkan yitbarek
  • 1,360
  • 2
  • 11
  • 28
0

Simply use setInterval function to call a function after an interval continuesly

$("#Initiate").click(function() {
 var intervalCall = window.setInterval(myCallback, 3000);
)} 

function myCallback(){
  if (coordinate=="GPS")
 {
   console.log("GPS go");
   lat_out = lat_in;
   long_out = long_in;
   alt_out = alt_in;
   console.log(lat_out, long_out, alt_out)
 }

}

For more information about setInterval, you can read here

Syam Pillai
  • 4,967
  • 2
  • 26
  • 42