Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:32:53+00:00 2026-06-13T09:32:53+00:00

please give me some clue for this javascript problem. I have a global variable

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:32:55+00:00Added an answer on June 13, 2026 at 9:32 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please give me some tips for this case. I have an iPhone APP which
Please give me some examples of jump table usage. I have seen this example
I have a problem, please give me some suggestion ? My problem is :
Anybody please give some useful links on this topic.i need to create a content
Could anyone give me some help with this please? The content needs to appear
Please give some suggestion ? Is it possible to do this?
This is a interview-questions. Please give some hints: Use vector to implement a method,
Can you please give me some comparison between C compilers especially with respect to
I am new to Software Development, could you please give me some input into
What does $+ in a GNU makefile mean? Also, please give me some good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.