开发者

Trying to perform a series of actions on page unload, but the pages unloads too fast to finish them

I have a series of actions I want to perform on page unload. Namely if a user is editing an input field, and they refresh or close browser or leave page, I want to save the contents开发者_运维知识库 of the field.

The actions don't include an AJAX call, so I can't just make it synchronous. It's actually saving to local storage, but the page unloads before the storage can take place. The code is correct, if I add an alert to the actions, the delay allows the rest of the code to finish before it even displays.

Any ideas?


use prevent default on unload

window.onunload = function(event){
   if(!event.handled){
     event.handled = true;
     event.preventDefault(); //stop the default action
     // and do your operations here
     //now close the window
  }else{
    //close the window
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜