How to run a function once when binding to multiple events that all trigger in Javascript?

前端 未结 5 1087
失恋的感觉
失恋的感觉 2020-12-19 20:23

I have a search input that listens to keyup and change to trigger an update of a listview via Ajax.

Looks like this:

input.on         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 20:47

    I came across this problem before and what I ended up doing is saving the request on an object upon creation and test for a previously set one to abort it if necessary. You'd have to edit the function that triggers the ajax call. For example:

    if ( ajaxRequest ) { 
      ajaxRequest.abort();
    }
    ajaxRequest = $.ajax({ ... }); // triggers ajax request and saves it
    

    If the widget function returns an ajax object then you can probably assign that to the variable instead of modifying the original ajax request.

提交回复
热议问题