jQuery: Can I call delay() between addClass() and

2019-01-01 04:41发布

Something as simple as:

$("#div").addClass("error").delay(1000).removeClass("error");

doesn't seem to work. What would be the easiest alternative?

标签: jquery delay
8条回答
十年一品温如言
2楼-- · 2019-01-01 05:30

AFAIK the delay method only works for numeric CSS modifications.

For other purposes JavaScript comes with a setTimeout method:

window.setTimeout(function(){$("#div").removeClass("error");}, 1000);
查看更多
路过你的时光
3楼-- · 2019-01-01 05:30

delay does not work on none queue functions, so we should use setTimeout().

And you don't need to separate things. All you need to do is including everything in a setTimeOut method:

setTimeout(function () {
    $("#div").addClass("error").delay(1000).removeClass("error");
}, 1000);
查看更多
登录 后发表回答