最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to reduce XMLHttpRequest time lag? - Stack Overflow

programmeradmin4浏览0评论

I use a XMLHttpRequest on a signup form to see if the username they entered has been taken already. The script runs fine, but I notice that there's some time delay when making the request - the webpage is frozen for a second, after sending the request.

Is there a way to have the request "in the background" and not cause any lag on the front end? I like Twitter's implementation: it shows an spinning hourglass icon while it's searching the database. How do I get something similar?

I use a XMLHttpRequest on a signup form to see if the username they entered has been taken already. The script runs fine, but I notice that there's some time delay when making the request - the webpage is frozen for a second, after sending the request.

Is there a way to have the request "in the background" and not cause any lag on the front end? I like Twitter's implementation: it shows an spinning hourglass icon while it's searching the database. How do I get something similar?

Share Improve this question asked May 29, 2009 at 1:27 user114060user114060
Add a ment  | 

2 Answers 2

Reset to default 7

You want to use asynchronous XHR -- currently you're performing a synchronous request, so the page has to freeze until the load has plete. Asynchronous XHR calls a callbck function you provide with the load status updates.

If memory serves you just need to do

 xhr.onreadystatechange = function() { 
     if (xhr.readyState === 4 && xhr.status === 200) loadFinished();
 }
 xhr.open(requestType, url,  true);

Where true makes the request asynchronous.

You need to specify that XMLHttpRequest operate asynchronously--in the background. Then you can provide a callback function so users can continue browsing until the operation is plete.

Many sites simply show an animated GIF while waiting for the operation to return, which probably gives the user the impression that you're looking for since it looks like something is happening. You can design and download one of those AJAX-spinning indicators easily at http://www.ajaxload.info/.

发布评论

评论列表(0)

  1. 暂无评论