Web Analytics Made Easy -
StatCounter Abort Ajax Request within class - CodingForum

Announcement

Collapse
No announcement yet.

Abort Ajax Request within class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Abort Ajax Request within class

    I'm getting a message saying request is undefined. Ajax runs fine, but how do I abort the process?
    Code:
    K12.Header = {
    
    	init: function() {
    var runningRequest = false;
    var request;
    
    		// init the type ahead boxes
    		//K12.initTypeAhead();
    
    $('.txt').keyup(function(e) {
    e.preventDefault();
    
            if(runningRequest){
               request.abort();
            }
    
    runningRequest=true;
    
    main_standard="";
    sub_standard="";
    standard="";
    g="";
    grade_level="";
    fields = {keyword:$(this).val(),main_standard:main_standard,sub_standard:sub_standard,grade_level:grade_level,archive:0};
    var json_vars = {
    action:'Search.instant_search',
    info:encodeURIComponent(JSON.stringify(fields))
    };
    	var request=K12.ajax(json_vars, function(data) {
    		var res = evalJSON(data);
    runningRequest=false;
                    // now spit out html items to the screen below
    //alert(res.name);
                   $('.right-container').html('');
                   $('.right-container').html(unescape(data));
                             
    											
    	});	
    						
    });
    
    });

  • #2
    I made two changes in bold. However, which allows the program to run, but does not kill the prior request. If I try to put the request.abort() outside of the ajax call, it claims it is not-existing?

    K12.Header = {

    init: function() {
    var runningRequest = false;
    var request=null;

    // init the type ahead boxes
    //K12.initTypeAhead();

    $('.txt').keyup(function(e) {
    e.preventDefault();

    if(runningRequest){
    request.abort();
    }

    runningRequest=true;

    main_standard="";
    sub_standard="";
    standard="";
    g="";
    grade_level="";
    fields = {keyword:$(this).val(),main_standard:main_standard,sub_standard:sub_standard,grade_level:grade_level ,archive:0};
    var json_vars = {
    action:'Search.instant_search',
    info:encodeURIComponent(JSON.stringify(fields))
    };
    var request=K12.ajax(json_vars, function(data) {
    if (request!=null) {request.abort();}
    var res = evalJSON(data);
    runningRequest=false;
    // now spit out html items to the screen below
    //alert(res.name);
    $('.right-container').html('');
    $('.right-container').html(unescape(data));


    });

    });

    });

    Comment


    • #3
      Solved:

      I had to pass this back into K12.header.request

      Comment

      Working...
      X