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 8749779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:45:05+00:00 2026-06-13T12:45:05+00:00

Thanks for taking the time to read this post. Finally built up enough courage

  • 0

Thanks for taking the time to read this post. Finally built up enough courage to ask a question here 🙂 Hope I ask my question in the correct way.

My problem is that I am trying to randomly animate “squares” around a page but am having an issue with the setInterval method.

You can find it here http://jsfiddle.net/xEMXh/

In the Init function I am trying to go though each ghost element and set it a interval function that will then make it animate in a random direction every X seconds.

for (ghostCount = 0; ghostCount < ghosts.length; ghostCount += 1) {
            var speed = LesMccutcheon.GhostHunter.GetSpeed($(ghosts[ghostCount]).attr('data-speed')),
                activeGhost = ghosts[ghostCount];
            window.setInterval(function() {
                LesMccutcheon.GhostHunter.MoveInDirection(activeGhost);
            }, speed);
        }

What is happening is only the last “ghost” element is animating. I know this is because in the for loop I am overriding the variable and by the time the first interval is called activeGhost is the last set in the for loop.

I tried using ghosts[ghostCount] directly in the set.Interval but it seems set interval cannot access this. I tried setting it to a variable inside the set.Interval anom function but it logged as undefined.

My only other half baked idea is to try and set an incremental var identifier but that seems quite counter productive and not a good way to go.

Thanks in advance for any advice!

Les

  • 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-13T12:45:06+00:00Added an answer on June 13, 2026 at 12:45 pm

    in your code the variable activeGhost will be overwritten for each loop.
    just put the calling of setInterval into a closure like this

    ( function (activeGhost) {
        window.setInterval(function() {
            LesMccutcheon.GhostHunter.MoveInDirection(activeGhost);
        }, speed);
    } ( activeGhost) );
    

    this way the reference to the correct element should be kept.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for taking the time to read this post. Hope those who are asking
Thanks for taking time to read this question. I am designing a 3-tier solution
Firstly, thanks for taking the time to read and possibly comment on this question.
thanks for taking the time out to read this post. I'm having trouble trying
Thanks for taking the time to read this. I have an unknown number of
Thanks everyone for taking the time to read this. I have styled my navigation
First, thanks in advance for taking the time to read through this. I have
Thanks in advance for taking the time to read my question. I'm using MySQL
Thanks for taking the time to read my post. I am looking for some
thanks for taking the time to read this. I have a JavaScript (jQuery) navigation

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.