How to cancel a jquery.load()?

前端 未结 7 1029
清酒与你
清酒与你 2020-11-29 07:30

I\'d like to cancel a .load() operation, when the load() does not return in 5 seconds. If it\'s so I show an error message like \'sorry, no picture loaded\'.

What I

7条回答
  •  死守一世寂寞
    2020-11-29 08:19

    If you load this code after JQuery has been loaded you will be able to call .load() with a timeout parameter.

    jQuery.fn.load = function( url, params, callback, timeout ) {
        if ( typeof url !== "string" ) {
            return _load.call( this, url );
    
        // Don't do a request if no elements are being requested
        } else if ( !this.length ) {
            return this;
        }
    
        var off = url.indexOf(" ");
        if ( off >= 0 ) {
            var selector = url.slice(off, url.length);
            url = url.slice(0, off);
        }
    
        // Default to a GET request
        var type = "GET";
    
        // If the second parameter was provided
        if ( params ) {
            // If it's a function
            if ( jQuery.isFunction( params ) ) {
                if( callback && typeof callback === "number"){
                    timeout = callback;
                    callback = params;
                    params = null;
                }else{
                    // We assume that it's the callback
                    callback = params;
                    params = null;
                    timeout = 0;
                }
            // Otherwise, build a param string
            } else if( typeof params === "number"){
                timeout = params;
                callback = null;
                params = null;
            }else if ( typeof params === "object" ) {
                params = jQuery.param( params, jQuery.ajaxSettings.traditional );
                type = "POST";
                if( callback && typeof callback === "number"){
                    timeout = callback;
                    callback = null;
                }else if(! timeout){
                    timeout = 0;
                }
            }
        }
    
        var self = this;
    
        // Request the remote document
        jQuery.ajax({
            url: url,
            type: type,
            dataType: "html",
            data: params,
            timeout: timeout,
            complete: function( res, status ) {
                // If successful, inject the HTML into all the matched elements
                if ( status === "success" || status === "notmodified" ) {
                    // See if a selector was specified
                    self.html( selector ?
                        // Create a dummy div to hold the results
                        jQuery("
    ") // inject the contents of the document in, removing the scripts // to avoid any 'Permission Denied' errors in IE .append(res.responseText.replace(rscript, "")) // Locate the specified elements .find(selector) : // If not, just inject the full result res.responseText ); } if ( callback ) { self.each( callback, [res.responseText, status, res] ); } } }); return this; };

    Not sure if you are interested in overwriting any "standard" JQuery functions, but this would allow you to use .load() the way you described.

提交回复
热议问题