0

I'm trying to set up a page loader for my website. Every time a user loads another page it will show the page loader until the page is done loading.

But for most of the cases, the web load so fast, and the page loader isn't shown. I tried to use setTimeout for 5 seconds but it make the loader show endlessly.

Anyone know how to fix this?

Here's my code:

$(document).ready(function(){
setTimeOut(()=> {
$('.loader').remove();
} , 5000); // after 5 sec it will remove.
});
/*Loading Screen*/
.loader {
width: 100%;
height: 100%;
top: 0;
left: 0;
position: fixed;
display: block;
opacity: 0.8;
background-color:#221e1e;
z-index: 99;
text-align: center;
}

.loader img {
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite;
position: absolute;
top: 50%;
left: 45%;
z-index: 100;
}
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
  }
  
  @keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="loader">
  <img src="img/logo/logo1.png" width="200px" height="75px">
</div>
SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
Kim San
  • 575
  • 7
  • 22

1 Answers1

1

javascript is Case Sensitive language

So it is sensitive to the letters uppercase and uppercase

The letter o is a large setTimeOut function that must be small, ie: setTimeout

$(document).ready(function(){
    setTimeout(()=> {
        $('.loader').remove();
    } , 5000); // after 5 sec it will remove.
});
/*Loading Screen*/
.loader {
width: 100%;
height: 100%;
top: 0;
left: 0;
position: fixed;
display: block;
opacity: 0.8;
background-color:#221e1e;
z-index: 99;
text-align: center;
}

.loader img {
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite;
position: absolute;
top: 50%;
left: 45%;
z-index: 100;
}
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
  }
  
  @keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="loader">
  <img src="img/logo/logo1.png" width="200px" height="75px">
</div>

It is also good to know these things:

The loader is usually installed when the screen is not fully loaded

$(document).ready(function () {

}

ready() Runs when the page is fully loaded You can write your loader outside of this function

Please read the following links:

https://api.jquery.com/ready/

Adding a page loader using jQuery

How to do jquery code AFTER page loading?

Description: Specify a function to execute when the DOM is fully loaded.

You can also use the following plugin for the loader

https://plugins.jquery.com/loader/

Erfan Bahramali
  • 392
  • 3
  • 13