我有一个网络应用程序必须多次调用服务器。到目前为止,我有一个很长的嵌套回调链;但我想使用 jQuerywhen
,then
等功能。但是,使用后我似乎无法让东西再次运行then
.
$
.when ($.get('pages/run-tool.html'))
.then (function (args)
{
// This works fine
alert(args);
$('#content').replaceWith (args);
$('#progress-bar').progressbar ({value: 0});
})
.then ($.get('pages/test.html'))
.done (function(args)
{
// This prints the same as the last call
alert (args);
});
我究竟做错了什么?我猜这是一些范围问题,因为我可以看到第二个get
正在执行的调用。使用两种不同的args
变量没有帮助,因为传递给完成函数的参数仍然是第一个get
要求。
作为更新:
使用现代 jquery (1.8+),您不需要初步的when因为get返回延期承诺。
Also, pipe已弃用。使用then反而。只需确保返回新 get 的结果,该结果将成为后续附加的 Promisethen/*done*/fail calls.
So:
$.get('pages/run-tool.html')
.then (function (args) { // this will run if the above .get succeeds
// This works fine
alert(args);
$('#content').replaceWith (args);
$('#progress-bar').progressbar ({value: 0});
})
.then (function() { // this will run after the above then-handler (assuming it ran)
return $.get('pages/test.html'); // the return value creates a new Deferred object
})
.done (function(args) { // this will run after the second .get succeeds (assuming it ran)
alert (args);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)