.delay () does not work when changing the background color of the <div>
I just study JQUERY and I played with a delay () I wrote a violin to show you ... What I'm trying to do is click a button, change the background color of the div and then after a few seconds switch the background color again. But when I try, it just switches to the second color and skips the first.
HTML:
<div class = "animation">
</div>
<button id = "change"> Click </button>
Here is the jQuery code:
$(document).ready(function(){
$("#change").click(function(){
$(".animation").css("background", "blue").delay(700).css("background", "red");
});
});
Here's the link:
+2
3 answers
delay only works for items in the queue (such as animations).
For anything else, use the regular old timer :
$("#change").click(function() {
var $el = $(".animation");
$el.css("background", "blue");
setTimeout(function () {
$el.css("background", "red");
}, 700);
});
+9
<div id="lock"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#lock').prepend('<tr><td>hello</td><td>cool</td><td>dad</td></tr>');
$("#lock tr").css('background-color','yellow');
$("#lock tr")
.delay(4000)
.queue(function() {
$(this).css("background-color","red").dequeue();
})
.fadeIn();
});
</script>
.
0