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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:48:51+00:00 2026-05-23T11:48:51+00:00

I have written a few custom animation functions as jQuery plugins using a setInterval()

  • 0

I have written a few custom animation functions as jQuery plugins using a setInterval() for the loop.

The functions seem to work fine by themselves but when I try to include more than one of them in the same page the timings and effects get all messed up and don’t render or complete properly!

I have done some scouting around on stackOverflow and google and from what I can gather the problem is that setInterval acts as a ‘global’ method / property of the window object and so when I have more than one on the page they end up overwriting each other!

Is this correct? And if so what is the solution? What does jQuery / plugins do to manage their own unique interval functions?

I was wondering if it’s possible to set a ‘local’ setInterval as a unique property to each of my plugin objects?

— OR —

If I have to create a function to manage multiple animation calls from various different plugins / functions to juggle them all with one global setInterval call — But I have no idea how to do this!

Can anyone help please?

—-edit—-

(function($){
    $.fn.scroller = function(options) {

        var defaults = {
            direction: 'left',
            distance: '',
            duration: 2000,
            interval: 2000,
            type: 'linear',
            startPos: '0'
        };
        var options = $.extend(defaults, options);

        return this.each(function() {
            parent = $(this).children('.scroller');
            $this = parent.children().first();

//          console.log(parent);
//          console.log($this);

            var o = options;
            var m = '';

            if(o.direction === 'left') m = 'marginLeft';
            if(o.direction === 'right') m = 'marginRight';
            if(o.direction === 'up') m = 'marginTop';
            if(o.direction === 'down') m = 'marginBottom';

//          console.log('Distance: ' + o.distance);
//          console.log('Duration: ' + o.duration);
//          console.log('Type: ' + o.type);
//          console.log('Start Pos: ' + o.startPos);
//          console.log('Interval: ' + o.interval);

            setInterval(function(){
                $this.animate({
                    m : o.distance
                },
                o.duration,
                o.type,
                function() { // OnComplete
                    $this.stop().css(m, o.startPos).appendTo(parent);
                });
            }, o.duration+o.interval);
        });
    };
})(jQuery);
  • 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-23T11:48:51+00:00Added an answer on May 23, 2026 at 11:48 am

    Update

    I think I see the problem. You define $this as a global variable. As such, calls to setInterval will all use the same $this variable. Try var $this = .... Same for parent. Also note that in the callback to $this.animate, “this” should refer to the dom element that was animated, so you could do $(this) to get a jQuery object for it.


    I think your problem might be that you loose context with setInterval. The this object may not be what you expect it to be. If you expect a variable to be present (because it was defined near where you call setInterval), it probably won’t be.

    Closures might help you. Try

    setInterval(function(){
        //code here
    });
    

    Rather than

    setInterval(myPreDefinedFunction);
    

    Also, try to make sure you are using locally defined variables in your setInterval callback rather than global ones:

    var myVar = 5;
    

    rather than

    myVar = 5;
    

    If you used the second method, you would end up sharing the variable between callbacks.

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

Sidebar

Related Questions

I have written a few MSBuild custom tasks that work well and are use
I have written a few Excel 2003/2007 add-ins using VSTO, and I usually end
I have written a few nearly identical functions, except for their names. For example:
I have a UserControl I've written to display a few properties from a custom
I have written a few benchmarks using Caliper . How can I run multiple
I am new to web serivce. I have written a few clients using AXIS2
I have written a few modules of code in Access vba. Each code runs
I have written a few Python tools in the past to extract data from
I have written quite a few Ruby programs, but if I need to show
I have written a mini web-server which needs to serve up a few static

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.