I have a click function, for when a user logs out
$('#logout').click(function(){
// update database and tell other users that this person has logged off
});
#logout is the id of a link (logout button)
The function will handle some ajax/php/database actions + sending a socketmessage and could easily take several miliseconds to complete
Can I trust that javascript/jquery completes that function before actually returning true (opens the link that is clicked)?
Or what would be a way do to make sure it completes it.. Starting with “return false”, and then only return true if it recieves an OK from php for example?
When an operation involves AJAX or any async operation, you can never do a “return”. Or else, you’d lose the essence of async.
Instead, hand over a callback to execute once it’s done. Or return a deferred object and have it listen if ajax is done.