开发者

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜