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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:19:49+00:00 2026-06-05T06:19:49+00:00

I am working on creating some dynamic content updating scripting and decided to make

  • 0

I am working on creating some dynamic content updating scripting and decided to make a jquery function that can be chained in order to be able to apply my code to multiple functions in a given page. Part of this, however, created some problems and I am hoping one of the more skilled js developers here might be able to help me out.

Basically I have the following JS code that will dynamically update content in the indicated element.

$.fn.updatecontent = function(vars){
    id=$(this).children(":first").attr("id");
    var i=0;
    change_contents = function(){
        $("#"+id).fadeOut('slow', function(){
            $(this).html(vars.data[i]);
            $(this).fadeIn('slow');
        });
        if(i+1 < vars.data.length){
            i++;                
        }else{
            i=0;
        }
    };
    setInterval(change_contents,vars.interval);
};

$(document).ready(function(){
    $("#button_one").updatecontent({
        interval: 5000,
        data: ["button 1 main content", "button 1 secondary content"]                            
    });   
    $("#button_two").updatecontent({
        interval: 7000,
        data: ["button 2 main content", "button 2 secondary content"]                            
    });             
});

My demo html looks like this

<section id="main_panel">
    <section id="button_one" class="button">
        <div id="button_one_content">button 1 main content</div>
    </section>
    <section id="button_two" class="button">
        <div id="button_two_content">button 1 main content</div>
    </section>
</section>​

The problem is, if I call this code on one element only it works fine, on the element I expect ( http://jsfiddle.net/C6h8s/10/ ). However, if I run it on two elements then it only updates the second element but with all 4 strings (I would link to this jsfiddle but I cant post more than two links at the moment and the first and last are the more important. Please uncomment the second call to see the wrong functionality). The problem appears to be with the setInterval function, because if I remove the call for that and call the function on both elements it will update both elements appropriately, though only one time due to the fact that I am no longer causing it to loop indefinitely over the available strings ( http://jsfiddle.net/C6h8s/11/ ).

Any input anyone might have on how to work around this problem would be 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-06-05T06:19:51+00:00Added an answer on June 5, 2026 at 6:19 am

    The second time you call the function, it overwrites id, since it is global. Use var:

    var id = .... ;
    

    DEMO

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

Sidebar

Related Questions

Okay I am working with some jquery creating dynamic popups. However, periodically the response
I am working on dynamically creating some JavaScript that will be inserted into a
I'm currently using Generics to make some dynamic methods, like creating an object and
I am creating a process to do some working. But the process is starting
I'm having troubles with creating a simple list (some expandable lists are already working).
I'm working on creating a self updating application and one issue I'm running into
I am working on creating a web app that will query event logs on
I'm working on creating a tests, and I can't figure out why the creation
I was working on creating some tables in database foo , but every time
I'm creating some data as a string in a webservice and returning that string

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.