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

  • Home
  • SEARCH
  • 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 4010478
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:00:18+00:00 2026-05-20T09:00:18+00:00

I have a Javascript object called TweenManager which contains an array of Tween objects.

  • 0

I have a Javascript object called TweenManager which contains an array of Tween objects. The TweenManager should call the step() method on each tween in the ‘tweens’ array and all the tweens should run at the same time.

However, what’s actually happening is that the TweenManager only runs one tween at a time, and doesn’t start the next one until the previous tween is complete.

Here’s the code for the tween manager

UPDATE: It might make more sense to look at it here

//Manage all tweens
function TweenManager(){

    this.tweens = new Array();
    this.timer;

    this.start = function(){
        this.timer = setInterval(this.run, 1, this);
    }

    // Loop through all tweens and call the step method
    this.run = function(myself){

        console.log(myself.tweens.length);

        // stop the interval if the tween array is empty
        if(myself.tweens.length == 0){
            clearInterval(myself.timer)
        }

        // loop through all tweens and call the step() method
            // !! Here's there the problem appears to be
        for(i = 0; i < myself.tweens.length; i++){

            thisTween = myself.tweens[i]        
            console.log(thisTween.element.attr('id'));  
            thisTween.step() // if I remove this, the line above logs the id's as expected

            // clean up if the tween is complete
            if(thisTween.t == thisTween.d){
                myself.tweens.splice(i, 1);
            }

        }

    }


    this.addTween = function(b,c,d,element,suffix, decimal){
        this.tweens.push( new Tween(b,c,d,element,suffix, decimal) )
    }

}

The problem appears to be in the for loop. I have a hunch that this might have something to do with passing in this in the setInterval, although it’s just a hunch, I don’t understand what the problem could be. I get confused with variable scopes and whatnot.

Here’s the Tween Object (Yup, ripped off form Robert Penner)

// Tween a number, add a suffix and insert it into an element
function Tween(b, c, d, element, suffix, decimal){

    this.t = 0;
    this.c = c;
    this.d = d;
    this.b = b;
    this.element = element;
    this.suffix = suffix;

    this.step = function(){

        if(this.t != this.d){

            this.t += 1 
            var flip = 1
            if (this.c < 0) {
                flip *= -1
                this.c *= -1
            }
            i = flip * (-Math.exp(-Math.log(this.c)/this.d * (this.t-this.d)) + this.c + 1) + this.b

            if(!decimal){
                this.element.html(Math.round(i) + this.suffix)
            }else{
                output = (Math.round(i * 10) / 10 + this.suffix)
                formattedOutput = ( output - Math.round(output) == 0 ) ? output + ".0" : output;
                this.element.html(formattedOutput)
            }

        }   
    }

}

And here’s the implementation

tweenManager = new TweenManager();
tweenManager.addTween(0,80,300, $("#el1"), "&deg;", false)
tweenManager.addTween(0,60,400, $("#el2"), "&#8217;", false)
tweenManager.addTween(0,12.5,300, $("#el3"), "", true)
tweenManager.start()

As always, any help, hinting or nudging the the right direction is greatly 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-05-20T09:00:19+00:00Added an answer on May 20, 2026 at 9:00 am

    I think the problem is that you are trying to use setInterval as some sort of fork() function which means that you should moving it from where it is to put it on the step itself so that you call:

    setInterval(thisTween.step, 1, ...
    

    That is how you can make your tweens run in fake ‘parallel’.

    However what I really think you want is the new HTML5 Web Workers feature; I think that is for exactly this kind of activity.

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

Sidebar

Related Questions

I have an included javascript file thats initializes an empty array object called widgets
In a javascript program there is an array (gData) that contains objects. Each of
I have a javascript object, which I've added a number of prototyped functions to,
I have a javascript object called 'element' of type HTMLInputElement that corresponds to a
I have a nested javascript object called data. Is there any way to iterate
I have added a Javascript interface object to a WebView using the addJavascriptInterface() method
I have a JavaScript array of objects like this: box[0] = {...} box[1] =
I hope someone can help me with this Javascript. I have an Object called
I have an object array called Persons and would like to sort by one
I have a JavaScript object called ShippingUI: function ShippingUI(){ ... } It has several

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.