Guys, please tell me how to make it so that while the file is being uploaded, a picture (loader) was shown, and after loading it was removed? Type as .ajaxStart.

    1 answer 1

    .ajaxSetup , .ajaxStart , .ajaxStop

    $(function() { var $loader = $('#loader'); $.ajaxSetup({ start: function() { $loader.show(); }, complete: function() { $loader.hide(); } // в случае успеха/ошибки }); }); 

    In my project I did something like the simplest stack based on a list so that the loader displayed while there was at least one request (in the background, they say, I work):

     var ajax_queue = [], $ajax_spinner = $('#ajax_spinner'); $.ajaxSetup({ timeout: 20000, // для кроссдоменных beforeSend: function(XHR) { ajax_queue.push(XHR); $ajax_spinner.show(); }, complete: function() { ajax_queue.pop(); $ajax_spinner.toggle(!!ajax_queue.length); // must be Boolean! } }); 

    Use!

    • Only it is necessary to add that these methods work only for $ .ajax () **. If you use ** $. Post () or $ .get () , then the parameters will not work. - Deonis
    • Just tried it - everything works fine for .get() - neoascetic
    • Hmm .. It is possible that there have been changes in the new versions. I tried it before - I did not roll it. Well, so much the better. - Deonis
    • @zhekonya are you talking about? Timeout most often needed for cross-browser queries. Otherwise, the browser resolves itself when an error occurred - neoascetic
    • I've done everything! thanks everyone! - zhekonya