jQueryのdeferedの使用方法を学んでいますが、$.whenと一緒に使用する際の問題に気づきました.notifyWith。
を使用せずに例を作成し$.when、.notifyWith完全に機能します
function a() {
var d = new $.Deferred,
$A = $('#A'),
$P = $('#P').progressbar();
setTimeout(function() {
$A.css('background-color', 'blue');
d.notifyWith($P, [.5]);
}, 2000);
setTimeout(function() {
$A.text('test');
d.notifyWith($P, [1]);
d.resolveWith($P, ['done']);
}, 4000);
return d.promise();
}
$('#G').click(function() {
a().progress(function(x) {
this.progressbar({
value: x * 100
});
}).done(function(x) {
alert(x)
});
});
デモ:http://jsfiddle.net/NTICompass/3DDSa/3/
内部は.progress、thisに設定されて$Pいるため、プログレスバーは正しく移動します。
setTimeout2つのアクションを別々の関数に分割したかったので、それを実行し$.when、promiseを1つに結合するために使用しました。
(function() {
var $P = $('#P').progressbar();
window.a = function() {
var d = new $.Deferred,
$A = $('#A');
setTimeout(function() {
$A.css('background-color', 'blue');
d.notifyWith($P, [.5]);
d.resolve('a()');
}, 2000);
return d.promise();
}
window.b = function() {
var d = new $.Deferred,
$A = $('#A');
setTimeout(function() {
$A.text('test');
d.notifyWith($P, [.5]);
d.resolve('b()');
}, 4000);
return d.promise();
}
}())
$('#G').click(function() {
$.when(a(), b()).progress(function(x, y) {
this.progressbar({
value: ((x || 0) + (y || 0)) * 100
});
}).done(function(x, y) {
alert(x + ' ' + y)
});
});
デモ:http://jsfiddle.net/NTICompass/3DDSa/16/
どういうわけかthis中は.progressありません $P。代わりに、それは据え置きオブジェクト(またはpromiseオブジェクト、私にはよくわかりません)です。なぜthis等しくないの$Pですか?