Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8303711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:36:29+00:00 2026-06-08T17:36:29+00:00

I have an asynchronous funcion (closing a socket) that I have to execute when

  • 0

I have an asynchronous funcion (closing a socket) that I have to execute when the user leaves the page. I’ve tried to do it through jQuery :

$(window).unload(function() {
   socket.disconnect();
});

However, this doesn’t work because the function is asynchronous and the client leaves the page as soon as it reach the end of the method, that’s to say instantly and the request to disconnect the socket is not performed.

We can provide a callback executed at the end of the socket.disconnect() function execution. Maybe this could help…

Is there a way in javascript to solve this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:36:30+00:00Added an answer on June 8, 2026 at 5:36 pm

    I’m going to stick my neck out and say that I don’t believe this is viable – at least not cross-browser.

    As one suggested, you can interrupt navigation via onbeforeunload but this is not implemented the same cross-browser, and even if you can interrupt it, you can’t (I believe) capture where the user was going, such that, in your socket closure callback, you could then forward them on.

    An unideal compromise might be to use onbeforeunload to at least prompt them to manually disconnect from the server. Many will; those that don’t you can pick up with server-side detection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function that I would like to execute after every asynchronous
I have an asynchronous job that sends an email to the user when it
I have a rails app that has asynchronous processing, and I'm having trouble getting
I have a contact us form that uses Ajax (i.e. relies on asynchronous requests).
I have an ASP.NET page that needs to make calls to multiple web services
I have a blocking function that executes an asynchronous MySQL query and returns the
I have a javascript function that enters a loop and issues an asynchronous ajax
I have a callback function that I'd like to execute after 2 separate $.getScript
I'm using jQuery $.when function to synchronize two $.getJSON asynchronous calls I have. Merging
I have a lot of JavaScript/ jQuery code blocks to handle asynchronous data processing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.