javascript - How to use setTimeout and get a return variable? -
I have a function geocode that works ... I want to implement a delay to prevent query boundary errors I am trying, and here is my javascript:
tmpResult = null; Set timeout (tmpResult = geocode (features [j], cities [j], state [j]),} 100);
variable tmpResult
is the result of my geocode .. However, tmpResult
is null
when I run the script .. am I missing?
Thanks! setTimeout ()
runs and then you may find this issue possible that you are trying to use tmpResult
is completely valid when < Code> setTimeout () setTimeout ()
before tmpResult
. Remember, this will run 100MMS in the future. Your other JavaScript will continue to run before that time.
What do you have to do to tmpResult
use the value setTimeout ()
or call some function and pass it in the value of tmpResult
.
setTimeout ()
is asynchronous and you can not synchronize while using it. You should have asynchronous code which means the value of means tmpResult
until the timer executes the callback so that you can not try to use it before the timer callback is activated is not valid.
If you try to do this, you have found:
var tmpResult = null; Set timeout (function (tmpResult = geocode (features [j], cities [j], state [j]);}, 100); console.log (tmpResult);
Because your console.log (tmpResult)
will be played before the statement timer becomes active, it will not work tmpResult
is not yet a value.
FYI, you should make sure that geocode ()
not even an asynchronous function because if it is, then you will have to code asynchronously with There are too.
Comments
Post a Comment