Как вы уже используете JQuery Я настроил пример без явного использования setTimeout
или setInterval
вообще - просто из любопытства :)
Вся магия делается с $.when
и deferred objects
var timer = 500,
animateLow = function (callback) {
$.when($("#test").animate({
height: '977px',
width: '1080px',
left: '49.5%',
top: '370px'
}, timer)).done(callback);
},
animateHigh = function (callback) {
$.when($("#test").animate({
height: '944px',
width: '1044px',
left: '50%',
top: '380px'
}, timer)).done(callback);
};
function heartbeatloop() {
animateLow(function() {
animateHigh(heartbeatloop);
});
}
heartbeatloop();
fiddle
Update
код для требования от комментариев
var element = $("#test"),
animate_timer = 1000, // duration of the animation
beat = [{
// method for animation
method: function() {
element.text("low");
return element.animate({height:'977px',width:'1080px',left:'49.5%',top:'370px'},animate_timer);
},
// time after which the next animation should start
timeout: 0
}, {
method: function() {
element.text("normal");
return element.animate({height:'944px',width:'1044px',left:'50%',top:'380px'},animate_timer);
},
timeout: 1000
}, {
method: function() {
element.text("high");
return element.animate({height:'995px',width:'1100px',left:'49.3%',top:'360px'},animate_timer);
},
timeout: 3000
}
];
function beatIt(idx) {
idx = idx || 0;
$.when(beat[idx].method()) // start the animation
.done(function() { // when finished start the next step in <timeout> milliseconds
setTimeout(function() {
beatIt((idx + 1) % beat.length); // next step with reset after last element
}, beat[idx].timeout);
});
}
beatIt(); // start the beating
Взгляните на это: http://jsfiddle.net/nRwkz/1/ – Alvaro
Спасибо, unfortunatlyt это еще не зацикливание;) – LexLusa
В любое время вы вызываете 'играть()' он установит ' tre_heartbeat_small' к новой анонимной функции - ничего более. При вызове 'heartbeatloop()' вы создаете анонимную функцию, которая будет вызывать 'play()', если она будет выполнена, но это не так. И последнее, но не менее важное: '' setTimeout' (http://mdn.beonex.com/en/DOM/window.setTimeout.html) может содержать более 2 аргументов, но что-либо за пределами второго будет передано как параметры переданной функции как первый аргумент. IE не может обрабатывать более двух аргументов. – Andreas