0

I'm trying to load a function after each span gets class one by one.

$("span.outline").each(function (index) {
 $(this).delay(300 * index).addClass("loaded");
});
$("span.outline").promise().done(function() {
 alert("All Loaded");
});
.outline{
 display: block;
 padding: 10px;
 margin-bottom: 10px;
 color: white;
 background-color: red;
}
.outline.loaded{
 background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="outline">Span Name</span>
<span class="outline">Span Name</span>
<span class="outline">Span Name</span>
<span class="outline">Span Name</span>

The problem is that in the beginning all spans must be red and become blue one by one.

Alert is working correctly. It waits until each delay time is finished.

Please help me to find a solution

asked Aug 19, 2021 at 9:31
5
  • 1
    .delay() only works for jquery animations, not as a general "wait". You could use setTimeout instead. Commented Aug 19, 2021 at 9:36
  • I used setTimeout and it works for elements but not for alert. Alert jumps immediately Commented Aug 19, 2021 at 9:40
  • 1
    Use setTimeout for the alert as well and set the timeout so it matches the sum of the other timeouts. As with .delay, .promise().done waits for jquery animations to complete. Commented Aug 19, 2021 at 9:51
  • Thank you I will try Commented Aug 19, 2021 at 9:59
  • This might help as an alternative approach: stackoverflow.com/questions/50532916/… Commented Aug 19, 2021 at 10:05

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.