使用jQuery防止过时的Ajax数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用jQuery防止过时的Ajax数据相关的知识,希望对你有一定的参考价值。

When running live filters on large, long scripts you can get old, stale results replacing your new, fresh ones. After all, a search for everything containing 'S" in a large database of city names can take a lot longer than "Seattle", so your script gets the results for "Seattle", displays then, then it gets the results for "S" and displays them, replacing the "Seattle" search you really wanted.

This script aborts the previous call before making the new one, effectively preventing that from happening. There are 2 caveats though.

1. The server may continue to process the request (depending on setup).
2. The error console logs it as an error, but not a fatal one and no alerts are thrown so the normal end user wont notice.

I have some more information available at the URL.
  1. function ajax_get(){
  2. if(getting!=''){getting.abort();}
  3. $('#loading_animation').fadeIn();
  4. filter = $.get('/path/to/ajax.file', function(data) {
  5. $('#ajax_container').html(data);
  6. $('#loading_animation').fadeOut();
  7. });
  8. }

以上是关于使用jQuery防止过时的Ajax数据的主要内容,如果未能解决你的问题,请参考以下文章

jquery ajax如何防止多次提交

在服务器端分页上防止来自 jquery 数据表的多个 ajax 调用

Javascript代码片段在drupal中不起作用

通过 Jquery 使用 Ajax 调用函数/数据库更新

使用 jQuery 和 AJAX 提交表单如果 return 设置为 false 则不会将值插入 SQL 以防止重定向

防止重复发送ajax请求