开发者

Proper jquery ajax loading image

开发者 https://www.devze.com 2023-03-18 20:06 出处:网络
I use this function on all my site ajax calls. My question is how would you rewrite it to have a proper pre-load image since mine is kind of a cheat and if the page takes too long to load or is 404 th

I use this function on all my site ajax calls. My question is how would you rewrite it to have a proper pre-load image since mine is kind of a cheat and if the page takes too long to load or is 404 that loading image never goes away.

function ajaxcall(my_url, my_div, my_data)
{
    $(my_div).append('<div style="text-align:center; position:absolute; top:0; left:0; width:100%;"><img src="/images/loading.gif" /></div>');
    $.ajax({ url: my_url,
        data: my_data,
        type: 'get',
        succ开发者_如何学Goess: function(output) 
            {
                $(my_div).html(output);
            }
    });
}

Regards


You could use the error option (see http://api.jquery.com/jQuery.ajax/) to write a handler function that deals with 404 or timeouts i.e removing the loading image and notifying the user somehow.


after that ajax call if it is a failure hide the div.

Otherwise you can put the image on another div.Initially it will be hide and in the start of function show the div and after ajax call hide the div whether it is success or failure.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号