jQuery Promise Example

Jquery When and promise

by Alvin Olavarrieta

HTML

<div id="id1"></div>
<div id="id2"></div>

CSS

div {
    border:1px black solid;
    width:200px;
    height:200px;
    background-color:red;
    margin:10px;
    position:relative;
}

JavaScript

var promise1 = $('#id1').animate({
    left: "+=50"
}, 1000);
var promise2 = $('#id2')
    .animate({
    left: "+=100"
}, 3000)
    .animate({
    top: "+=100"
}, 2000);


$.when(
promise1,
promise2).then(function () {
    alert('p');
});