javascript - create a callback function instead of using a flag var to control when a specific function is done -
i have code snipet this:
var nbrprevart = $("#accordion > div .accordionpanel").size(); var processing = false; if (nbrprevart == 0) { processing = true; getarticlespreview();//fetches articles first } //loop makes browser wait until finishes "getarticlespreview()" while(!processing) { } //getarticlespreview() finished can safely execute step $.ajax({ type: "get", url: "http://mydomain.com/somepage.htl", success: function(data) { alert(data);} }); //----------------------------------- function getarticlespreview() { //lenghty operation processing = false; }
don't practical because using loop make wait until function competely executed perform next step.
is there way define callback message called when first operation done , have second step ( $.ajax call) inside run properly?
thank in advance! teixeira
you create callback yourself, using apply
function. juste have add callback
parameter getarticlespreview, , put function inside callback.
this :
function makeajax() { $.ajax({ type: "get", url: "http://mydomain.com/somepage.htl", success: function(data) { alert(data);} }); } function getarticlespreview(callback) { //lenghty operation callback.apply(this) } var nbrprevart = $("#accordion > div .accordionpanel").size(); if (nbrprevart == 0) { getarticlespreview(makeajax); }
Comments
Post a Comment