我有一个很长的同步函数,我想用jQuery.when和/或jQuery.Deferred()来实现它。
我挣扎着,没有找到任何例子!
示例(打字稿):
public Bind(cell: number) {
console.log("entering!");
$.when(this.validateCell(cell)).done( () => {
console.log("finished!");
});
console.log("out!");
}
private validateCell(cell: number): JQueryPromise<any> {
const dfd = $.Deferred();
console.log("0");
for (let i = 0; i < 1000000000; i++) {
const t = i;
}
console.log("1");
dfd.resolve();
return dfd.promise();
}
我明白了:
entering!
0
1
finished!
out!
虽然我想得到这个:
entering!
out!
0
1
finished!
提前谢谢!
答案 0 :(得分:0)
答案 1 :(得分:-2)
尝试添加setTimeout
private validateCell(cell: number): JQueryPromise<any> {
const dfd = $.Deferred();
console.log("0");
setTimeout(function(){
for (let i = 0; i < 1000000000; i++) {
const t = i;
}
})
console.log("1");
dfd.resolve();
return dfd.promise();
}