admin管理员组文章数量:1415111
When the user refreshes the page, defaultView()
is called, which loads some UI elements. $.address.change()
should execute when defaultView()
has finished, but this doesn't happen all the time. $.address.change()
cannot be in the success:
callback, as it's used by the application to track URL changes.
defaultView();
function defaultView() {
$('#tout').fadeOut('normal', function() {
$.ajax({
url: "functions.php",
type: "GET",
data: "defaultview=true",
async: false,
success: function (response) {
$('#tout').html(response).fadeIn('normal');
}
});
});
}
$.address.change(function(hash) {
hash = hash.value;
getPage(hash);
});
I'm at a loss as to how to make $.address.change()
wait for defaultView()
to finish. Any help would be appreciated.
When the user refreshes the page, defaultView()
is called, which loads some UI elements. $.address.change()
should execute when defaultView()
has finished, but this doesn't happen all the time. $.address.change()
cannot be in the success:
callback, as it's used by the application to track URL changes.
defaultView();
function defaultView() {
$('#tout').fadeOut('normal', function() {
$.ajax({
url: "functions.php",
type: "GET",
data: "defaultview=true",
async: false,
success: function (response) {
$('#tout').html(response).fadeIn('normal');
}
});
});
}
$.address.change(function(hash) {
hash = hash.value;
getPage(hash);
});
I'm at a loss as to how to make $.address.change()
wait for defaultView()
to finish. Any help would be appreciated.
-
You can't do this. You've got two asynchronous events going on here: the fadeOut of #tout, and the AJAX request. A synchronous AJAX request is the least desirable piece of Javascript code I can think of. The only way of doing this is to put
$.address.change()
in the success callback of the AJAX request, so another approach is needed. – Matt Commented Apr 25, 2010 at 22:11 -
async: false,
is there by error - I was trying how it affected the script. Didn't make any difference, and I removed it. – Wurlitzer Commented Apr 25, 2010 at 22:19
3 Answers
Reset to default 3Call it in the success
or plete
callback. Using delay
for timing a callback is unreliable at best. You might even need to put the call to it in the callback to the fadeIn
function inside of the success
callback.
It doesn't have to be defined inside the success callback, just executed. Both contexts will still be able to use it.
I too was told that because of async you can't make javascript "wait" -- but behold an answer :D ...and since you're using jQuery, all the better:
use jQuery's bind and trigger. I posted an answer to a similar problem at How to get a variable returned across multiple functions - Javascript/jQuery
One option is to hide the $.address (I'm guessing this is a drop-down list) via css, and show it inside the success callback from the ajax method.
本文标签: javascriptWait for function to finish before executing the restStack Overflow
版权声明:本文标题:javascript - Wait for function to finish before executing the rest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745202618a2647464.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论