troubleshooting Question

Trouble with setTimeout and an array

Avatar of saturation
saturation asked on
JavaScript
5 Comments2 Solutions218 ViewsLast Modified:
I have an array of well over 30 elements, and I'm trying to do an on-screen alert of a group of 10 every 3 seconds (i.e., the first 10 elements alert one time in the first array, the next 10 elements alerted 3 seconds after that, etc.).  My code below seems to be alerting the same piece of the array every time. What am I doing wrong?

function delayCode(locations2) {
      var i,j,temparray,chunk = 10;

      for (i=0; i < locations2.length; i+=chunk) {
          temparray = locations2.slice(i, i+chunk);
          setTimeout(function() {alert(temparray)}, 3000);

      }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros