0

please give me some clue for this javascript problem. I have a global variable markers. And try to push every marker to markers. But the problem is , after push to markers. i was trying to alert the value inside function and outside function. the result is totally different. markers inside function give me array of marker, but markers outside stay empty. Why i got different value of markers global variable?

This is the snippet of my code:

for (var i = 0; i < netotal; i++) {
    setTimeout(function () {
        marker = new google.maps.Marker({
            position: pos[iterator],
            map: map,
            draggable: false,
            animation: google.maps.Animation.DROP,
            icon: neicon  
        });
        iterator++;
        markers.push(marker);console.log(markers);
    }, i * 50);  
}

alert (markers);

Thank you for your kind help or clue.

3
  • Inside your question you state that the alert inside the function is on "markers" and outside the function is on "marker". Maybe some typing mistake? Also it would help to share the rest of the code too. I can not see when the alert will be triggered which is a crucial thing to know here. Is the alert triggered right after the for loop? Then the markers will be empty indeed because it will be filled when the delayed functions execute. Commented Oct 25, 2012 at 7:40
  • @rmoorman , yap i made some typing mistake. i 've added some line on the snippet. the console result and alert is different. Commented Oct 25, 2012 at 7:48
  • Then it is caused by the fact, that the markers will be filled later on (due to setTimeout). Your edit makes that clear too. You could confirm this by replacing the alert with something like setTimeout(function() { console.log(markers); }, netotal * 50); Commented Oct 25, 2012 at 8:04

2 Answers 2

1

You're pushing to markers in a function that's called using setTimeout, so the push won't happen until some time later. But you're calling alert(markers) immediately, before any of the timeouts have occurred. So the array is empty at that time.

UPDATE:

To see the final contents of markers, you need another setTimeout:

setTimeout(function() { alert(markers); }, netotal*50);
Sign up to request clarification or add additional context in comments.

4 Comments

ahh i see, so how do i run the next script after all the marker loaded?
Maybe use another setTimeout with a higher timeout. Or if
set it to 0 but my markers[0] still undefined outside function
JavaScript is not multitasking, the functions invoked by setTimeout won't run until the script ends, even with timeout 0. You need to do the outside call with another setTimeout. See UPDATE.
1

Depends on the scope, to access the global markers use window.markers.

1 Comment

trying to update to window.markers.push(marker) is not helping :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.