I would like to display a loading spinner but ONLY if "my_code_here" takes longer than X milliseconds to execute.
I have managed to do this:
<button onClick='my_long_function()'></button>
<div class="spinner" style="display: none;"></div>
my_long_function: function()
{
$(".spinner").show(200, function()
{
my_code_here
my_code_here
my_code_here
$(".spinner").hide();
}
}
It works but it shows the spinner even when the code takes less than 100ms, so it's showing up during the blink of an eye and I have issues making that look good.
Note that it's not ajax waiting for a server answer, it's all client side.
I tried stuff with setTimeout and clearTimeOut by reading like 5 stackoverflow questions but either it doesn't seem to trigger or the spinner stays forever.