admin管理员组文章数量:1333419
I am trying to call another function after load() is pleted. I first tried to check with alert(), however, I see the alert before load() is done. I am able to successfully load content in load() function. However, alert() is empty.
$(document).ready(function(){
load();
$.when(load()).done(function(){
alert((document.getElementById('item').innerHTML));
});
function load()
{
$("#item").load("/somepage.aspx", function (response, status, xhr){
if ( status == "error" ) {
var msg = "error: ";
$( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
}
});
};
});
I am trying to call another function after load() is pleted. I first tried to check with alert(), however, I see the alert before load() is done. I am able to successfully load content in load() function. However, alert() is empty.
$(document).ready(function(){
load();
$.when(load()).done(function(){
alert((document.getElementById('item').innerHTML));
});
function load()
{
$("#item").load("/somepage.aspx", function (response, status, xhr){
if ( status == "error" ) {
var msg = "error: ";
$( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
}
});
};
});
Share
Improve this question
edited Jul 15, 2015 at 18:39
Barmar
783k56 gold badges547 silver badges660 bronze badges
asked Jul 15, 2015 at 18:36
smtpsmtp
1592 gold badges4 silver badges11 bronze badges
3 Answers
Reset to default 1You need load()
to return a promise, and then you pass this promise to $.when
.
.load()
doesn't return a promise, it returns the jQuery collection that it was called on (for chaining purposes). You can rewrite load()
using $.get()
instead, which returns a jQXHR
, which implements Deferred
(that's jQuery's promise class).
var p = load();
$.when(p).done(function() {
alert($("#item").html());
});
function load() {
return $.get("/somepage.aspx", function (response, status, xhr){
$("#item").html(response);
if ( status == "error" ) {
var msg = "error: ";
$( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
}
});
}
You don't actually need to use $.when
, you can just write:
p.done(function() { ... });
You can use callback functions and update your code to following
$(document).ready(function(){
load(function(){
alert((document.getElementById('item').innerHTML));
});
function load(callback)
{
$("#item").load("/somepage.aspx", function (response, status, xhr){
if ( status == "error" ) {
var msg = "error: ";
$( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
}
if(callback) {
callback();
}
});
};
});
You can use the new Promises API which is already implemented in most major browsers.
var jsonPromise = new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
if ( /* everything turned out fine */ ) {
resolve("Stuff worked!");
}
else {
reject(Error("It broke"));
}
});
jsonPromise.then(function(data) {
console.log(result); // "Stuff worked!"
}, function(err) {
console.log(err); // Error: "It broke"
});
本文标签: javascriptcall a function after another function is completedStack Overflow
版权声明:本文标题:javascript - call a function after another function is completed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742326710a2453870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论