0

I have two javascript function, fn1 will close the pop-up window and fn2 will refresh the parent page. And callback fucntion is called in the button click of popup window.

function fn1()
{
   this.window.close();
}

function fn2()
{
  Refreshpage()//Refreshin the page logic
}

function callback()
{
   var callbacks = $.Callbacks();
   callbacks.add(fn1);
   callbacks.add(fn2);
}

Issue is before closing the popup, page is refreshing (i.e before fn1 finish to execute the code fn2 is executing).

How to make wait till fn1 to finish executing its code, before fn2 starts its execution.

Simon Arnold
  • 15,849
  • 7
  • 67
  • 85
user472269
  • 367
  • 1
  • 4
  • 19

1 Answers1

0

Try adding callbacks.add(fn2) within fn1 , calling callbacks.fire(fn2) after .close() called

var w = window.open("", "w");

var callbacks = $.Callbacks();

function fn2() {
    alert("complete")
}

function fn1() {        
    callbacks.add(fn2);
    w.close();
    callbacks.fire()
}

function callback() {
    callbacks.add(fn1);
    callbacks.fire();
}

callback()

See also $.queue() , where array of functions could be passed , next function in queue list could be called using next() within current function . If next() not called , next function would not be called

var w = window.open("", "w");

var obj = {};

$.queue(obj, "list", [fn1, fn2])


function fn2() {
    alert("complete") //Refreshin the page logic
}

function fn1(next) {
    w.close();
    next()
}

function callback() {
   $.dequeue(obj, "list")
}

callback()
guest271314
  • 1
  • 15
  • 104
  • 177