call a function after completion of previous function
I am trying to achieve this:
function doThis()
{
firstFunction(function()
{
closeWindow();// close window only after firstFunction() completes
});
}
where doThis() is called from a button. The firstFunction() is an AJAX call after the completion of which second non-AJAX function alert() has to be called. Somehow the code doesnt work as intended. The firstFunction() works and is being called fine. But window.close doesnt seem to be called.
Any advice guys ? No jquery codes please.
update:
let me elaborate this a bit. the function doThis() is being called from a submit link.
<a href="#" name="sumbit" onClick='doThis()'><img src="img.png"/></a>
After the press of this link, the form needs to be submitted and this popup windows needs to be closed. firstFunction() POSTs this form using XMLHttpRequest and is working fine when i use it like this:
<a href="#" name="sumbit" onClick='firstFunction()'><img src="img.png"/></a>
Using doThis() i am trying to post the form first and then only close the form popup but havent been able to.
update 2
as delnan mentioned,turns out that firstFunction wasnt calling the function that was passed to it as an argument. ma开发者_高级运维de these changed and it works well now:
function firstFunction(callback)
{
//do ajax stuff
callback(); //calls closeWindow()
}
thank you guys !
Are you positive that your AJAX code works correctly. I mean how do you know that, since the callback function is not executed? As of invokation itself you can pass just alert without a wrapper function (to firstFunction that is).
Declare your function like so:
function firstFunction(callback) {
// setup XMLHttpRequest / IE equivalent
}
and in the readystatechange event handler, call the callback:
if(this.readyState == 4 && this.status == 200) {
// other stuff
callback();
}
If you are already doing this then you probably have an error somewhere in firstFunction
and you have to show its implementation.
Or doThis
is never called.
精彩评论