为什么浏览器在进行2次Ajax调用时会锁定?
我现在正在努力工作几个小时,但我无法让它工作。 请帮忙!
我有2个Ajax函数,其中1将需要10秒才能完成,而另一个函数将在2 mili秒后完成。 第二个function将通过回调调用。
- 这两个function都是独立运作的
- 第1个function运行后,第2个function的输出被阻止,直到第1个function结束。
但这是不是反对异步Ajax理论? 不应该同时运行和输出两个function吗?
这是JQuery:
$('#tbut').click(function(){ // Declare variables var go1 = true; var go2 = true; var z = 1; // Fire 1st Ajax call a(); // 1st function with callback function a() { $.ajax({ url: 't2' }) .always(function() { console.log('t2 always'); $('#tdiv').html(z); // update DIV z = z + 1; if (go1) { if (go2) { b(); } setTimeout(function(){a()},2); // callback } }); }; // 2nd function function b() { go2 = false; $.ajax ({ url: 't1' }) .done(function() { console.log('fin'); $('#tdiv').html('fin'); // update DIV go1 = false; }); }; return false; });
控制器动作:
public function actionT1() { // make it take a little longer... $i=0; while ($i < 10) { $i++; sleep(1); } exit; } public function actionT2() { // do nothing here at the moment... exit; }
风景:
now
我解决了(非常明显的问题)。 我声明我的php函数没有正确的返回值。 退出该函数等同于“return false” ,因为我没有success()或error()处理程序,它导致浏览器做奇怪的事情(大多数只是完全锁定视图)。
更新控制器function:
public function actionT1() { // make it take a little longer... $i=0; while ($i < 10) { $i++; sleep(1); } return true; } public function actionT2() { // do nothing here at the moment... return true; }
和
function a() { $.ajax({ url: 't2' }) .always(function() { console.log('t2 always'); $('#tdiv').html(z); // update DIV z = z + 1; if (go1) { if (go2) { b(); go2 = false; } setTimeout(function(){a();},200); // callback } }); }; function b() { $.ajax({ url: 't1' }) .done(function() { console.log('success'); $('#tdiv').html('this works!'); // update DIV }) .error(function(data) { console.log('fail'); $('#tdiv').html(data.responseText); // update DIV }) .always(function(){ go1 = false; }); };