简体   繁体   English

识别最新的 api 调用响应并忽略旧的调用响应

[英]Identify latest api call response and ignore old call responses

I'm working on a search page where the user enters text in a search input textbox and it makes an ajax call with the input box text for each and every character update and updates the UI.我在一个搜索页面上工作,用户在搜索输入文本框中输入文本,它使用输入框文本为每个字符更新和更新 UI 进行 ajax 调用。 but I'm facing a problem in identifying last API call response.但我在识别最后一个 API 调用响应时遇到了问题。 suppose, user types 'ABC' and it's making three API calls for,假设,用户输入“ABC”,它正在发出三个 API 调用,

  1. 'A' '一种'
  2. 'AB' 'AB'
  3. 'ABC' 'ABC'

I'm getting responses in the order of 2,3,1.我按 2,3,1 的顺序收到回复。 finally, UI is having results for 'A' search text instead of 'ABC'最后,用户界面的结果是“A”搜索文本而不是“ABC”

I'm not advised to use promise/async methods to resolve this issue and also cant not make any serverside changes in API call.我不建议使用 promise/async 方法来解决这个问题,也不能在 API 调用中进行任何服务器端更改。

$.ajax({
    url: "search_api",
    success: function(){
    // update UI
    }
});

First, whenever a change is made to the input text box, you should immediately empty the area holding the search results as a prelude to making the AJAX call.首先,每当对输入文本框进行更改时,您应该立即清空保存搜索结果的区域,作为进行 AJAX 调用的前奏。 The AJAX response should include the original search argument and if that search argument is not currently in the input text box, the response should be thrown away. AJAX 响应应包含原始搜索参数,如果该搜索参数当前不在输入文本框中,则应丢弃该响应。 In that way the order in which the responses are returned should be irrelevant.这样,返回响应的顺序应该无关紧要。 While you are outputting the search results, you should disable the input text box to ensure that the input text box and search results are always in sync.在输出搜索结果时,您应该禁用输入文本框,以确保输入文本框和搜索结果始终同步。 Caching previous AJAX calls in a dictionary whose key is the search argument and the value are the corresponding search results will, of course, save you from making extra AJAX calls.在一个字典中缓存以前的 AJAX 调用,其键是搜索参数,值是相应的搜索结果,当然可以避免进行额外的 AJAX 调用。

When you have no control over the specs for the API call and it is impossible to have the response tagged with the original search argument, drastic measures will need to be taken:如果您无法控制 API 调用的规范,并且无法用原始搜索参数标记响应,则需要采取严厉措施:

First, and this applies to even the original best-case scenario where the response can be tagged with the original search argument, you should not be making the API call until there has been no keystroke activity for some amount of time (eg a half second).首先,这甚至适用于可以用原始搜索参数标记响应的原始最佳情况,在一段时间内(例如半秒)没有击键活动之前,您不应该进行 API 调用)。 So, if the user is a fast typist, you should be waiting until there is some pause in the typing.所以,如果用户是一个快速打字员,你应该等到打字有一些暂停。 Otherwise, by time the response comes back it will probably no longer be valid.否则,到响应返回时,它可能不再有效。 In this particular case where the original search argument cannot be returned in the response, you will have no choice but to make the call but then postpone any other requests for when the current AJAX call completes.在响应中无法返回原始搜索参数的这种特殊情况下,您别无选择,只能进行调用,然后在当前 AJAX 调用完成时推迟任何其他请求。 In that way, there is never more than one outstanding AJAX call.通过这种方式,永远不会有超过一个未完成的 AJAX 调用。 When the current call completes, there is a possibility that the results are no longer relevant because the input text box has changed and you will need to do a another API call.当前调用完成后,结果可能不再相关,因为输入文本框已更改,您将需要执行另一个 API 调用。 The duration of keyboard inactivity you should be waiting for will depend on how fast the system responds to the AJAX call: You can get away with shorter pauses the faster the system responds.您应该等待的键盘不活动持续时间将取决于系统响应 AJAX 调用的速度:系统响应速度越快,您就可以避免暂停时间越短。

The more I think about this, there is no reason why you shouldn't be able to tie your multiple asynchronous responses that are returned back in arbitrary order to the original request.我想得越多,就没有理由不能将以任意顺序返回的多个异步响应与原始请求联系起来。 Here is an example using jQuery.这是一个使用 jQuery 的示例。 I wrote an echo server, test4.py , that is posted with a variable x and simply echoes the value back.我编写了一个回显服务器test4.py ,它与变量x一起发布并简单地回显该值。 Function make_request is passed argument x and posts this using jQuery to the echo server.函数make_request被传递参数x并使用 jQuery 将其发布到回显服务器。 When the asynchronous response comes in, it is able to tie it back to the original request argument by using a function closure for the callback argument to the done method on the request object created by the ajax call:当异步响应进来时,它能够通过使用回调参数的函数闭包将其与原始请求参数绑定到由 ajax 调用创建的请求对象上的done方法:

function makeRequest(x) {
    var req = jQuery.ajax({
        'method': 'POST',
        'url': 'http://localhost/ron/test/test4.py',
        'data': {x: x}
        });
    req.done(function(msg) {
        console.log(msg +',' + x);
    });
}

The callback function specified to the done method just logs the returned message from the echo server and the original x argument.指定给done方法的回调函数只记录从回显服务器返回的消息和原始x参数。 To kick things off on the document ready event, we have:为了启动文档就绪事件,我们有:

$(function() {
    for (let x = 0; x < 5; x++) {
        makeRequest(x);
    }
});

And an examination of the log shows:对日志的检查显示:

test2.html:16 4,4
test2.html:16 3,3
test2.html:16 1,1
test2.html:16 2,2
test2.html:16 0,0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM