简体   繁体   中英

How Can I Reliably Complete Asynchronous Operations on Firefox browser close?

I'm writing a browser extension for Firefox that uses IndexedDB to save local data. When the browser closes, I'd like to write the latest data out to my IndexedDB. However, IndexedDB is entirely asynchronous, and it appears that Firefox closes before the asynchronous writes complete. (I can see the database file being created and the journaling file getting discarded when the browser closes.) Is there a way to reliably complete asynchronous operations when Firefox is closing?

You could use a eventlistener (you may already have it to write to your database) to listen to the window close event and preventDefault until you finish storing your things and then you close the window on the callback.

window.addEventListener("close", function(e) {
    e.preventDefault(); 
    //Save your things
    //Call window.close on the callback
    window.close();
}, false);

There's also a different question on stackoverflow that may have a better way to listen to the application close instead of window close. but the process would be similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM