0

I'm unable to make use of setTimeout() function in XUL 7.0, it is not working.

while closing XUL window, i have created one event listener as like below.

   window.addEventListener("close",function(event) {
   
   try
   {
   setTimeout(function() { alert("bip"); }, 2000);
    
   }
   catch(e)
   {
    //alert(e.message);
   }
  },false);
   

But the alert is not coming.

Any help will be highly appreciated!

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Naresh
  • 53
  • 2
  • 1
    Try to bind the `'onbeforeunload'` event instead... although, actually, if you do a 2 second timeout at the close event, you can't expect it to get triggered (you can't expect the window to live 2 sec after the close event) – Šime Vidas May 04 '12 at 15:25
  • And by "XUL 7.0" you probably mean XUL in Firefox 7.0? Or XULRunner 7.0? – Wladimir Palant May 04 '12 at 20:32
  • Sorry for the delay, Sima : even if i give 10 sec also it is not working as expected Wladimir : it is XULRunner 7.0 – Naresh May 09 '12 at 14:20

1 Answers1

3

setTimeout won't block. The function will finish, the window will close, and two seconds later the timer will run out. At that stage the execution environment will have gone away (as the window is closed), so nothing will happen.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It won't even run - closing the window kills all timeouts associated with it. But the reason is the same, the context of these timeouts has gone away. – Wladimir Palant May 04 '12 at 20:33
  • Got the solution. if we just keep setTimeout() in close function, it is not closing instead i have used preventDefault() which makes close function to not close and perform my operation and then used currentWinObj.close() to close the XUL window. ` window.addEventListener("close",function(event) { try { preventDefault(); setTimeout(function() { alert("bip"); }, 2000); window.close(); } catch(e) { //alert(e.message); } },false);` Thank you for all of your help! :) – Naresh May 09 '12 at 14:21