Note that there are some explanatory texts on larger screens.

plurals
  1. POJS setInterval within a function that will be called simultaneously causing wrong element to be updated
    primarykey
    data
    text
    <p>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. </p> <p>Basically I have the following JS code that will dynamically update content in the indicated element. </p> <pre><code>$.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 &lt; 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"] }); }); </code></pre> <p>My demo html looks like this</p> <pre><code>&lt;section id="main_panel"&gt; &lt;section id="button_one" class="button"&gt; &lt;div id="button_one_content"&gt;button 1 main content&lt;/div&gt; &lt;/section&gt; &lt;section id="button_two" class="button"&gt; &lt;div id="button_two_content"&gt;button 1 main content&lt;/div&gt; &lt;/section&gt; &lt;/section&gt;​ </code></pre> <p>The problem is, if I call this code on one element only it works fine, on the element I expect ( <a href="http://jsfiddle.net/C6h8s/10/" rel="nofollow">http://jsfiddle.net/C6h8s/10/</a> ). 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 ( <a href="http://jsfiddle.net/C6h8s/11/" rel="nofollow">http://jsfiddle.net/C6h8s/11/</a> ). </p> <p>Any input anyone might have on how to work around this problem would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload