Results 1 to 5 of 5

Thread: AJAX Aborting

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    454
    Thanks
    8
    Thanked 44 Times in 42 Posts

    Default AJAX Aborting

    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:
    Code:
    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;
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
            }
    	};
    	var settings = $.extend({}, defaults, options, consti);
    	var request = null;
    
    	function runAjax() {
    		if ( request != null && request.readyState != 4 ) {
    			console.log('Should Abort');
    			request.abort();
    			request = null;
    		} 
    			
    		console.log(request);
    		request = $.ajax(settings);
    		console.log(request);
    	}
    
    	this.set = function(options) {
    		settings = $.extend({}, defaults, settings, options, consti);
    	};
    
    	this.get = function() {
    		return settings;
    	};
    
    	this.run = function() {
    		if ( settings.url != null ) {
    			runAjax();
    		} else {
    			console.log('Ajax Error: Page is null.');
    		}
    	};
    }
    
    polling.add('onclick');
    The call:
    Code:
    polling.onclick.run();
    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.
    Last edited by Deadweight; 09-13-2017 at 03:33 PM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,372
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    Well, path is not defined. And there seems to be other confused or maybe just confusing code. For example:

    Code:
    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):

    Code:
    $('selector').click(function(){$.ajax({options});});
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    454
    Thanks
    8
    Thanked 44 Times in 42 Posts

    Default

    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.
    Last edited by Deadweight; 09-13-2017 at 03:33 PM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,372
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    function forceWrite(location) {
    if ( location.indexOf("/") === 0 ) {
    location = location.substring(1);
    }
    history.pushState({}, "Pushing", path + location);
    polling.onclick.run();
    }
    "location" is a proprietary word (or something like that). It means (unless somehow qualified):

    window.location.href

    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:

    http://api.jquery.com/jQuery.ajaxSetup/

    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):

    http://api.jquery.com/jQuery.fn.extend/

    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.
    Last edited by jscheuer1; 09-13-2017 at 10:30 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Deadweight (09-14-2017)

  6. #5
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    454
    Thanks
    8
    Thanked 44 Times in 42 Posts

    Default

    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.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. Resolved best practice login (secure data) via ajax - POST JSON AJAX
    By lse123 in forum JavaScript
    Replies: 12
    Last Post: 01-01-2013, 12:22 AM
  2. Replies: 5
    Last Post: 08-09-2012, 12:10 PM
  3. Looping And User Aborting
    By M2com in forum PHP
    Replies: 6
    Last Post: 05-09-2012, 01:49 AM
  4. Resolved Text and Image Crawler aborting operation in IE
    By msaz87 in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 05-08-2009, 12:11 AM
  5. [Ajax] Ajax Messages (Ajax Demonstration)
    By iMarc in forum Submit a DHTML or CSS code
    Replies: 2
    Last Post: 03-22-2007, 03:31 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •