我想在页面加载时生成 4 个圆圈。但是,即使该函数被调用了 4 次,我也只看到了一次。我想知道我缺少什么部分。是因为当函数完成时,它开始的所有超时都被清除了吗?
function getRandom(min, max) {
return Math.floor((Math.random()*max)+min);
}
function generateCircle(leftPos, topPos, zIndex) {
$circle = $('<div class="cercleAnime"></div>');
$('body').append($circle);
var self = this;
self.leftPos = leftPos;
self.topPos = topPos;
$circle.css({
'left' : self.leftPos+'px',
'top' : self.topPos+'px',
'z-index' : zIndex
});
$circle.animate({'width' : '300px', 'height' : '300px'}, {
duration : 5000,
progress: function() {
newLeft = self.leftPos - $(this).width()/2;
newTop = self.topPos - $(this).height()/2;
$(this).css({
'left' : newLeft+'px',
'top' : newTop+'px'
})
},
done: function() {
$(this).remove()
}
} );
}
function generateCircles(numberOfCircles, intervalBetweenCircles) {
leftPos = getRandom(1,1000);
topPos = getRandom(100,400);
zIndex = 1000;
timeoutTime = intervalBetweenCircles;
for(var i = 0; i < numberOfCircles; i++) {
zIndex--;
setTimeout(function(){generateCircle(leftPos, topPos, zIndex)}, timeoutTime);
timeoutTime += intervalBetweenCircles;
}
}
$(function() {
generateCircles(3, 2000);
generateCircles(3, 2000);
generateCircles(3, 2000);
generateCircles(3, 2000);
});
这是一个 jsfiddle:http: //jsfiddle.net/etiennenoel/BwyH7/