|
var consData = function() { |
|
return $.getJSON('http://ergast.com/api/f1/2012/constructors.json?&callback=?'); |
|
}; |
|
|
|
var driverData = function() { |
|
return $.getJSON('http://ergast.com/api/f1/2012/drivers.json?&callback=?'); |
|
}; |
|
|
|
var fadeTheLamp = function() { |
|
return $('.green-lamp').each(function(i) { |
|
$(this).delay(i * 1000).animate({'opacity': 0}, 1000); |
|
}).promise(); |
|
}; |
|
|
|
$('#go').on('click', function() { |
|
// When Constructors data arrived, let us know. |
|
// Don't append DOM node this way in actual project! Please... |
|
consData().done(function() { |
|
$('<div>', { |
|
'text': 'Constructors data has been successfully done', |
|
'class': 'alert alert-info' |
|
}).appendTo('#info'); |
|
}); |
|
|
|
// When Drivers data arrived, let us know. |
|
// Don't append DOM node this way in actual project! Please... |
|
driverData().done(function() { |
|
$('<div>', { |
|
'text': 'Drivers data has been successfully done', |
|
'class': 'alert alert-info' |
|
}).appendTo('#info'); |
|
}); |
|
|
|
// When Drivers data, Constructors Data, and the lamps all gone |
|
// Only if those are completed, then inform us. |
|
// if one of those are failed, alert us. |
|
$.when(consData(), driverData(), fadeTheLamp()).then( |
|
//success callback |
|
function(consResp, driverResp) { |
|
$('<div>', { |
|
'text': 'All async operation above has been done successfully', |
|
'class': 'alert alert-info' |
|
}).appendTo('#info'); |
|
}, |
|
//error callback |
|
function() { |
|
$('<div>', { |
|
'text': 'Something went wrong!', |
|
'class': 'alert alert-error' |
|
}).appendTo('#info'); |
|
} |
|
); |
|
}); |
|
|
|
$('#reset').on('click', function() { |
|
$('.green-lamp').attr('style', ''); |
|
$('#info').empty(); |
|
}); |