jQuery, display loading page div only if the page takes more than 2 seconds to load

拜拜、爱过 提交于 2019-12-09 13:07:36

问题


This is my first question, so hopefully I will give enough details. I have the following code in 4 pages on a website:

$(document).ready(function() {
  $('#page_loading').slideDown(500);
});

jQuery(window).load(function () {
  setTimeout(function() {$('#page_loading').slideUp(500);}, 1500);
});

What I am I trying to achieve: When browsing thorough the 4 pages, have the #page_loading div slide down only if the page takes more than 2 seconds to load. If the pages takes less than 2 seconds to load (was visited before and most of the images are cached) then the loading div won't show up.

At this point, even if the page takes less than one second to load that loading div still appears and disappears, and it's pretty annoying.

Thank you, Cristian.


回答1:


Will this do the trick?

var showTimeout = setTimeout(function() {
    $('#page_loading').slideDown(500);
}, 2000);

jQuery(window).load(function () {
    clearTimeout(showTimeout);
    $('#page_loading').slideUp(500);
});


来源:https://stackoverflow.com/questions/15069955/jquery-display-loading-page-div-only-if-the-page-takes-more-than-2-seconds-to-l

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!