Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

when a user click the cross button on top of window tab, will automatically update on mysql database.

I have a situation above:

when a user go to view.php page i will update my mysql database one of the column "view". when a user click the view another page or click the close button on top of window tab i will update the database "view" to "no" since to check the user is leaving the page should be done on client side i'm using javascript then using ajax to update my database.

below is my code

 window.onbeforeunload = confirmExit;

                                    function confirmExit()
                                    {
                                    //update the database
                                    }

Since i'm closing the page it will closing the connection to the server and therefore it might won't wait Ajax call to finish. Is there more efficient way to make sure it can update the database when user leaving the current page