user471011
user471011

Reputation: 7374

JavaScript: How long the browser can expect to perform JS before opening a new page?

Page has several links.

I need to track click on the links the user.

That is, when user click's on a link sent to Ajax request to my server, my server processes the request and returns some data. Further, these data are processed on the client side as needed and sent to the server statistics.

All of these requests and data processing require a certain time.

My question is practical: how long the browser can expect to perform all these actions? there is a limit in time?

Just now I have the following options:

When you click the link, using js return false. Perform all necessary functions, and only then, to execute the opening of the new window.

Thus I avoid problems with the expectation of the browser.

Upvotes: 1

Views: 64

Answers (1)

lonesomeday
lonesomeday

Reputation: 237975

If you want to ensure that the link is not followed until your AJAX request has completed, you should do a synchronous request. For instance:

link.onclick = function () {
    var xhr = new XMLHTTPRequest(); // and the cross-browser equivalents

    xhr.open('GET', 'yourscript.cgi?data1=foo&data2=bar', false);
    // false means do the request synchronously

    xhr.send(); // won't return till the request is complete

    // you can access the data as xhr.responseText or xhr.responseXML
};

This is a much more reliable (and probably faster) way of doing the necessary notifications than waiting a certain period of time.

Upvotes: 1

Related Questions