Solution
By simply setting the animation
property to none
, you can easily stop the spinner:
document.getElementById('spinner').style.animation = 'none';
Demo
Check out this code snippet below for a live demonstration. The spinner will come to a halt after running for 2s
:
// Get the spinner element
var spinner = document.getElementById('spinner');
// Display the spinner
spinner.style.display = 'inline-block';
// Set a timeout for 2 seconds
setTimeout(function() {
// Stop the spinner animation
spinner.style.animation = 'none';
}, 2000);
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="40222f2f34333432213000746e766e72">[email protected]</a>/dist/css/bootstrap.min.css">
<span class='spinner-border spinner-border-sm text-primary' role='status' aria-hidden='true' id='spinner' style='display: none'></span>
<!-- jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="513b202434232811627f667f60">[email protected]</a>/dist/jquery.slim.min.js"></script>
<!-- Popper JS -->
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33435c434356411d594073021d02051d02">[email protected]</a>/dist/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a6ababb0b7b0b6a5b484f0eaf2eaf6">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
Edits
Edit #1: How to display a full circle
If the spinner is not initially displaying as a full circle, one workaround is to change the class to an icon from FontAwesome
when stopping the spinner. Here's how you can achieve that:
First, include the Font Awesome CDN:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
In FontAwesome 6, the circle icon is represented by
<i class="fa-regular fa-circle"></i>
. So in JavaScript, replace the current
className
of the spinner with
'fa-regular fa-circle'
like so:
document.getElementById('spinner').className = "fa-regular fa-circle";
Demo
In this demo, the spinning will stop after 2s
and the icon will be changed (I've used the check
icon and added the green color using the text-success
class - feel free to customize as needed).
Edit #2: A More Effective Approach to Handling Animations
@Yogi suggested another solution in the comments which may be more advanced, but considering the original intention was to change the icon at the end of the spin rather than restart the animation, the earlier method suffices. However, if restarting the animation is desired, you can also utilize
spinner.style.animationIterationCount
where setting it to
0
stops and setting it to
Infinite
restarts the animation.