lilmer Posted June 12, 2013 Share Posted June 12, 2013 (edited) 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 June 12, 2013 by lilmer Quote Link to comment https://forums.phpfreaks.com/topic/279063-javascript-abort-function-not-working/ Share on other sites More sharing options...
kicken Posted June 12, 2013 Share Posted June 12, 2013 You're going to give a better explanation than "not working". What are you expecting to happen? How are you determining if it is working or not? Are there error messages? Quote Link to comment https://forums.phpfreaks.com/topic/279063-javascript-abort-function-not-working/#findComment-1435518 Share on other sites More sharing options...
lilmer Posted June 13, 2013 Author Share Posted June 13, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279063-javascript-abort-function-not-working/#findComment-1435664 Share on other sites More sharing options...
nogray Posted June 13, 2013 Share Posted June 13, 2013 You need to make the request a global variable. e.g. // global variable var request; function onSearch(value){ if(request != null) request.abort(); // no "var" infront of request request = $.ajax({ Quote Link to comment https://forums.phpfreaks.com/topic/279063-javascript-abort-function-not-working/#findComment-1435678 Share on other sites More sharing options...
kicken Posted June 13, 2013 Share Posted June 13, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279063-javascript-abort-function-not-working/#findComment-1435696 Share on other sites More sharing options...
lilmer Posted June 13, 2013 Author Share Posted June 13, 2013 Alright, thank you for the example of mostly for the idea you've shared. I'm going to try your example. Quote Link to comment https://forums.phpfreaks.com/topic/279063-javascript-abort-function-not-working/#findComment-1435712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.