javascript - Execute code only if AJAX request takes a certain amount of time? -



javascript - Execute code only if AJAX request takes a certain amount of time? -

in web application, i'm using $.ajax() request load info database , display in browser. during execution of request, display loading results ... message this:

$.ajax({ // ... beforesend: function() { $('#loading-results-message').show(); }, complete: function() { $('#loading-results-message').hide(); } });

this works fine. however, if there not much info load, request takes fraction of second. in case, message displayed fraction of sec well. animation happens it's hard recognize it. therefore, great if possible display message if request takes amount of time, i.e. seconds @ to the lowest degree not fraction of second. possible somehow? way, i'm using django on server side, if should matter.

use settimeout found timer, cancel timer when request completes:

var desired_delay = 2000; var message_timer = false; $.ajax({ // ... beforesend: function() { message_timer = settimeout(function () { $('#loading-results-message').show(); message_timer = false; }, desired_delay); }, complete: function() { if (message_timer) cleartimeout(message_timer); message_timer = false; $('#loading-results-message').hide(); } });

documentation

settimeout on mdn - https://developer.mozilla.org/en-us/docs/dom/window.settimeout cleartimeout on mdn - https://developer.mozilla.org/en-us/docs/dom/window.cleartimeout

javascript ajax jquery

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -