Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's an optimized version of your code, but like bvukelic said the whole concept would probably be more efficient with canvas.</p> <p>The problem I see with the code anyway is that delay and fade actions are indeed called synchronously, but the timeout is executed asynchronously, meaning that after a while you have multiple streams of actions on the same object (and this up to a hundred times).</p> <p>So the solution would be to append the repeated timeout as callback to the .fadeOut() action.</p> <pre><code>$(function() { var $win = $(window), winWidth = $win.width(), winHeight = $win.height(), showLoop = function(prob) { var fadeInDuration = prob.appear == 'fade' ? 1000 : 0; $('#'+prob.id) .delay(prob.interval*1000) .fadeIn(fadeInDuration) .delay(prob.duration*1000) .fadeOut(1000, function() { window.setTimeout(function() { showLoop(prob); // synchronously repeated callback }, 1000); }); }; $.ajax({ type: 'get', url: 'problems.xml', /* ...which contains like 99 problems */ dataType: 'xml', success: function(problems) { $('problem', problems).each(function() { var $t = $(this), probName = $('name', this).text(), prob = { type: $t.attr('type'), appear: $t.attr('appear'), name: probName, id: probName.replace(/\s/g, '').toLowerCase(), intensity: (parseInt($('intensity', this).text()) + 5) * 10, interval: parseInt($('interval', this).text()), duration: parseInt($('duration', this).text()), pos: { top = Math.random()*winHeight-(prob.intensity/2), left = Math.random()*winWidth-(prob.intensity/2) } }; $('&lt;p&gt;') .append('&lt;span&gt;'+prob.name+'&lt;/span&gt;') .attr('id', prob.id) .addClass(prob.type) .width(prob.intensity) .height(prob.intensity) .css({ top: prob.pos.top, left: prob.pos.left, }) .appendTo('body'); showLoop(prob); }); } }); }); </code></pre>
 

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