Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a loader in Javascript waiting for a function to end?

I have a function in Javascript (in the browser, not node.js) that it takes a lot of time to commit. I want that while the function is being processed, the browser will present a loader, so the user can understand that he/she has to wait.

like image 977
CrazySynthax Avatar asked Dec 05 '22 16:12

CrazySynthax


1 Answers

create a loader first :

<div class="loader" id="loader">
</div>

Then add the loader class in your CSS(css file of your loader class) :

 .loader {
    border: 16px solid #f3f3f3; /* Light grey */
    border-top: 16px solid #3498db; /* Blue */
    border-radius: 50%;
    width: 120px;
    height: 120px;
    animation: spin 2s linear infinite;
 }

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.hide-loader{
display:none;
}

Add this js code to your JS file after your function executes completely,just hide the loader.

     $('#loader').addClass("hide-loader");
like image 169
Akshay Tilekar Avatar answered Jan 18 '23 02:01

Akshay Tilekar