beforeunload event during window close Vs meteor-auto-reload

久未见 提交于 2019-12-24 03:01:24

问题


I use the follow function to detect window close event to perform some cleanup tasks:

    window.addEventListener('beforeunload', function(e) {
          console.log("window close");
          // perform cleanup
    }

Unfortunately I also get this event during meteor auto-reload (for ex. when there is change in code) during which I don't want to perform cleanup.

How do I differentiate between the two situations? (I have a peculiar usecase where I have to differentiate between the two situations)


回答1:


You would have to tap into the Meteor onmigrate api which could help you distuinguish the two:

Client side code:

var hotcodepush = false;

Reload._onMigrate(function () {
    hotcodepush = true;
    return [true];
});


window.addEventListener('beforeunload', function(e) {
      if(!hotcodepush) console.log("window close");
      if(hotcodepush) console.log("Hot code reload");
}



回答2:


Without knowing meteor. I would try to find the point which is executed just before the auto-update (if possible) and define a abort event|variable|... I hope this helps



来源:https://stackoverflow.com/questions/21940888/beforeunload-event-during-window-close-vs-meteor-auto-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!