View Full Version : Resolved AJAX Aborting

09-12-2017, 03:33 PM
Currently, I have a class set up in Ajax that calls certain pages.
I have a page that has a 5-second sleep to it (because I was checking something); however, the issue is if that page is still getting called I want to cancel and stop the 5-second page call (or any page for that matter) and start the other ajax call.

The set up:

var polling = {
add : function(name, obj) {
this[name] = new ajax(obj);
return this;

var timeout;
function ajax(options) {
var instance = $(this);
var consti = {
url: path + 'sniffer.php',
complete: function() {request = null;console.log(request);}
var defaults = {
method: 'POST',
data: {},
dataType: 'json',
cache: 'false',
async: true,
success: function(reply) {console.log(reply);},
error: function(jqXHR, textStatus, errorThrown){
if(textStatus == 'abort') return;
var settings = $.extend({}, defaults, options, consti);
var request = null;

function runAjax() {
if ( request != null && request.readyState != 4 ) {
console.log('Should Abort');
request = null;

request = $.ajax(settings);

this.set = function(options) {
settings = $.extend({}, defaults, settings, options, consti);

this.get = function() {
return settings;

this.run = function() {
if ( settings.url != null ) {
} else {
console.log('Ajax Error: Page is null.');


The call:


sniffer.php will auto detect the page that is required to load.

Any suggestions to cancel and abort the 5-second sleep in a page would be awesome. Thanks.

09-12-2017, 04:59 PM
Well, path is not defined. And there seems to be other confused or maybe just confusing code. For example:

var instance = $(this);

Now, instance doesn't appear to get used elsewhere anyway, but this in this case appears to be the prototypical function, so $'ing it would be meaningless. Another thing is, if you're using jQuery (looks like it), it should be 'click' not 'onclick'. There are number of other things like this, or that just seem over thought and more complicated than needed.

Since it looks like you're using jQuery. Why not use its tried and true methods? Something like (in simplest terms):


09-12-2017, 05:27 PM
The issue was not javascript. It was actually my PHP. I was starting a session with a sleep. Sessions will continue to sleep and lock until it's finished. That was the issue.

Thanks for your help.

09-13-2017, 10:01 PM
OK, I was going to look at that when I had a chance, but I see you've changed it. One thing I did notice earlier:

function forceWrite(location) {
if ( location.indexOf("/") === 0 ) {
location = location.substring(1);
history.pushState({}, "Pushing", path + location);

"location" is a proprietary word (or something like that). It means (unless somehow qualified):


which is, as I'm pretty sure you know, both a 'setter' and a 'getter', so pretty powerful, considering it's the current page location/address. Now the way you have it there is qualified, but perhaps not enough in all circumstances. And even if it is in that case, once you start using it as a parameter/variable, sooner or later a situation may easily arise where it's misinterpreted for its proprietary meaning. Many people us "loc" or really any other non-proprietary word for that very reason.

Other thoughts I had on your code that I hope may be helpful - You seem to be wanting to graft generic prototypical javascript constructs on or around jQuery. And that can be done. What I think many who do that don't realize is that jQuery is already prototypical in its own way, so often these efforts are redundant. As an example, I see you listing defaults for the call you eventually make via $.ajax. These can be established with:


Whatever you include in the object for that function will become the default(s) for any subsequent AJAX call executed via jQuery regardless of whether or not it's via $.ajax or any of the shorthand/specific jQuery AJAX methods, and can/will be overridden by specific settings for any subsequent call. So all that was already available. There are many other situations like this in jQuery, and although it's not required one use them, generally once you've loaded jQuery, using what it gives you will be more efficient and simpler to deal with in the long run.

These are just general suggestions, and while I'm at it I should try to explain that using a classic javascript prototypical function is largely ubiquitous with jQuery. The usually more efficient analogous (yet decidedly different) method is to extend the jQuery object itself with a new tailor made function, applicable to any selector (one at a time, a combined selector, or various ways, like any native jQuery function) and capable (depending upon how you define it) of accepting additional parameters and/or an object of parameters. See (for more info):


Took me awhile to get that one. And, as I said, one can still use a classic javascript prototypical function* with jQuery if one wants to.

*by classic javascript prototypical function I mean where you define a function and then call instances of it using the new keyword.

09-14-2017, 01:09 PM
Thanks for that information. I didn't see that I used location and switched it to loc.
I'm actually trying to not use JQuery in some instances and slowly start using javascript. (At least in some instances).
I haven't updated the page to display any of the changes. EG I dropped using $.ajax and created a personal HTTP request.