Jump to content

Recommended Posts

I've got this

<input type="text" class="search admin-managers" onkeyup="onSearch(this.value)">

and my Javascript funcion

function onSearch(value){
        
       if(request != null) request.abort();
       
       var request =  $.ajax({
                type: "POST",
                url: url+'searchManager',
                data: {search:value},               
                success: function(html)
                {    
                  
                   $('#theResult').html(html);
                }
                }); 
   
}

I want to abort the past ajax call so it will not affect the current ajax call but the function ".abort()" is not working. Is their a problem with my code?

Edited by lilmer
Link to comment
https://forums.phpfreaks.com/topic/279063-javascript-abort-function-not-working/
Share on other sites

Okay then.

 

Every input that the user make an ajax call is triggered, so what I want to happen is if he/she continue inputting text on the text box it will abort the previous ajax call but not the last call of course. I thought using javascript function "abort" and putting it before the ajax call it will stop the previous request but it ain`t happening.

 

Thanks.

Ideally you should not perform the request until there is a pause in the typing that way you don't end up just creating a bunch of requests only to immediately abort them. You can do this by using setTimeout to delay the function for a period of time. Eg:

var timeout=null;
var request=null;
function onSearch(value){
    if (timeout) clearTimeout(timeout);   

    timeout = setTimeout(function(){
        if (request) request.abort();

        request =  $.ajax({
            type: "POST",
            url: url+'searchManager',
            data: {search:value},               
            success: function(html){    
                $('#theResult').html(html);
            }
        }); 
    }, 150);
}
That will delay the request for 100ms so that someone typing quickly will not cause a bunch of requests to be created then aborted. Only a single request will be made once they have a small break in typing.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.