admin管理员组文章数量:1401849
I'm trying to figure out an ajax call... I need the call to hold up and finish loading HTML contents, before going over once again (it is triggered by keyup event)... Otherwise returned HTML is actually loaded multiple times. In my case the ajax call returns HTML which then executes a JS function. But when it gets loaded multiple times, JS function is executed twice = bad.
Here is my code (alert boxes are only for help...):
$(document).ready(function() {
var isProcessing = false;
$('#productSearchInput').keyup(function() {
var productSearchInput = $(this).val();
var dataString = 'searchInput='+ productSearchInput;
var script_url = '../ajax/service-search.php';
if(productSearchInput.length > 3 && isProcessing === false) {
$.ajax({
type: 'GET',
url: script_url,
data: dataString,
beforeSend:
function() {
isProcessing = true;
alert('Processing input: ' + productSearchInput;
},
success:
function(server_response) {
$('#searchresultdata').html(server_response).show('fast', function() { alert('Currently processing...'); } );
},
error:
function() {
alert('ajax call failed...');
},
plete:
function() {
isProcessing = false;
alert('Processing done');
}
});
}
return false;
});
});
What is wrong is, that the "Processing done" alert appears before the "Wait for processing" alert... And then after "Processing done" alert has showed, the "Wait for processing" alert shows.
I need my HTML server_response to show, before pleting the call and setting isProcessing = false;...
How do I achieve that?
I'm trying to figure out an ajax call... I need the call to hold up and finish loading HTML contents, before going over once again (it is triggered by keyup event)... Otherwise returned HTML is actually loaded multiple times. In my case the ajax call returns HTML which then executes a JS function. But when it gets loaded multiple times, JS function is executed twice = bad.
Here is my code (alert boxes are only for help...):
$(document).ready(function() {
var isProcessing = false;
$('#productSearchInput').keyup(function() {
var productSearchInput = $(this).val();
var dataString = 'searchInput='+ productSearchInput;
var script_url = '../ajax/service-search.php';
if(productSearchInput.length > 3 && isProcessing === false) {
$.ajax({
type: 'GET',
url: script_url,
data: dataString,
beforeSend:
function() {
isProcessing = true;
alert('Processing input: ' + productSearchInput;
},
success:
function(server_response) {
$('#searchresultdata').html(server_response).show('fast', function() { alert('Currently processing...'); } );
},
error:
function() {
alert('ajax call failed...');
},
plete:
function() {
isProcessing = false;
alert('Processing done');
}
});
}
return false;
});
});
What is wrong is, that the "Processing done" alert appears before the "Wait for processing" alert... And then after "Processing done" alert has showed, the "Wait for processing" alert shows.
I need my HTML server_response to show, before pleting the call and setting isProcessing = false;...
How do I achieve that?
Share Improve this question edited Oct 9, 2012 at 12:37 EibergDK asked Oct 9, 2012 at 11:17 EibergDKEibergDK 5641 gold badge8 silver badges19 bronze badges2 Answers
Reset to default 1This is normal behavior. Using show
method requires some time for animation, so callback is called after plete. Why not to simplify your code to something like this:
$.ajax({
type: "GET",
url: script_url,
data: dataString,
beforeSend: function() {
isProcessing = true;
alert('Processing input: ' + productSearchInput);
},
success: function(server_response) {
alert('Wait for processing...');
$('#searchresultdata').html(server_response).show('fast', function() {
alert('Processing done');
isProcessing = false;
});
},
error: function() {
alert('ajax call failed...');
}
});
Why wouldn't you use jQuery.Deferred object, instead of isProcessing?
Please check the code in this case (something like this):
var isProcessing = $.Deferred();
$.ajax({
type: "GET",
url: script_url,
data: dataString,
beforeSend: function() {
alert('Processing input: ' + productSearchInput);
},
success: function(server_response) {
alert('Wait for processing...');
$('#searchresultdata').html(server_response).show('fast', isProcessing.resolve);
},
error: function() {
alert('ajax call failed...');
}
});
$.when(isProcessing).then(function(){alert('Processing done');});
本文标签: javascriptAjax call to complete successbefore actually completing the callStack Overflow
版权声明:本文标题:javascript - Ajax call to complete success, before actually completing the call? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744320751a2600482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论