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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:29:41+00:00 2026-06-13T16:29:41+00:00

I am trying to animate some elements on a page using JavaScript (CSS animations

  • 0

I am trying to animate some elements on a page using JavaScript (CSS animations with sprites will not work for what I need to do).

I am currently doing something like this;

function animate_element(current_id)
{
    var next_id = parseInt(current_id, 10) + 1;
    $('#lighthouse')[0].src = '/element_' + next_id + '.png';

    if (next_id >= 8) {
        next_id = 0;
    }     

    setTimeout(function() {
        animate_element(next_id);
    }, 750);
}

Technically this works, but this is going to be one of many animations on the page that will be doing something similar and I am worried this is an inefficient way of doing it.

I know best practice is to use clearTimeout() before calling setTimeout, but I don’t know how to record the setTimeout and recursively pass it into itself (if that makes any sense!).

Any guidance on the best practice way of doing this would be much appreciated.

  • 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-13T16:29:42+00:00Added an answer on June 13, 2026 at 4:29 pm

    “I know best practice is to use clearTimeout() before calling setTimeout…”

    For what you’re doing, there’s no reason to call clearTimeout() since the next call will never happen until the last one has been executed.

    At that point, there’s nothing to clear.


    FWIW, your code can be shortened a bit:

    function animate_element(current_id) {
        current_id = parseInt(current_id, 10);
    
        function cycle() {
            document.getElementById('lighthouse').src = '/element_' + current_id + '.png';
            current_id = ++current_id % 8
            setTimeout(cycle, 750);
        }
    }
    
    animate_element(0);
    

    Or if there are several that are the same, just with a different ID, you can make it reusable like this:

    function animate_element(id, idx) {
        idx = parseInt(idx, 10);
    
        function cycle() {
            document.getElementById(id).src = '/element_' + idx + '.png';
            idx = ++idx % 8
            setTimeout(cycle, 750);
        }
    }
    
    animate_element('lighthouse', 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using DOJO to create some animations. I'm trying to animate an h1 inside
I am using flot to do some graphing and I am trying to animate
I am in need of some help, i am trying to use css3 animations
I'm trying to move some elements on the page, and during the time the
I'm trying to animate some IMG elements by updating the top margin with some
I'm trying to animate some stuff using the usual UIView class methods. I'm calling
I'm trying to animate some UIImageViews for a 2D game. Using [UiView begin/commit animation]
I've been trying to animate DOM elements using transitions alone, since I've read they're
I am trying to animate a div moving 200px horizontally in JavaScript. The code
I'm trying to animate the font size of some text: $(p).delay(500).animate({ font-size: +=50 },

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.