由于您已经在使用 jQuery,我已经设置了一个示例,但没有明确使用setTimeout
或setInterval
根本没有使用 - 只是出于好奇:)
所有的魔法都是用$.when
延迟对象完成的
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();
小提琴
更新
评论中要求的代码
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