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

JqueryDelay

Jquery Problem Overview


Something as simple as:

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

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

Jquery Solutions


Solution 1 - Jquery

You can create a new queue item to do your removing of the class:

$("#div").addClass("error").delay(1000).queue(function(next){
	$(this).removeClass("error");
	next();
});

Or using the dequeue method:

$("#div").addClass("error").delay(1000).queue(function(){
	$(this).removeClass("error").dequeue();
});

The reason you need to call next or dequeue is to let jQuery know that you are done with this queued item and that it should move on to the next one.

Solution 2 - Jquery

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);

Solution 3 - Jquery

I know this this is a very old post but I've combined a few of the answers into a jQuery wrapper function that supports chaining. Hope it benefits someone:

$.fn.queueAddClass = function(className) {
    this.queue('fx', function(next) {
        $(this).addClass(className);
        next();
    });
    return this;
};

And here's a removeClass wrapper:

$.fn.queueRemoveClass = function(className) {
    this.queue('fx', function(next) {
        $(this).removeClass(className);
        next();
    });
    return this;
};

Now you can do stuff like this - wait 1sec, add .error, wait 3secs, remove .error:

$('#div').delay(1000).queueAddClass('error').delay(2000).queueRemoveClass('error');

Solution 4 - Jquery

jQuery's CSS manipulation isn't queued, but you can make it executed inside the 'fx' queue by doing:

$('#div').delay(1000).queue('fx', function() { $(this).removeClass('error'); });

Quite same thing as calling setTimeout but uses jQuery's queue mecanism instead.

Solution 5 - Jquery

Of course it would be more simple if you extend jQuery like this:

$.fn.addClassDelay = function(className,delay) {
	var $addClassDelayElement = $(this), $addClassName = className;
	$addClassDelayElement.addClass($addClassName);
	setTimeout(function(){
		$addClassDelayElement.removeClass($addClassName);
	},delay);
};

after that you can use this function like addClass:

$('div').addClassDelay('clicked',1000);

Solution 6 - Jquery

Delay operates on a queue. and as far as i know css manipulation (other than through animate) is not queued.

Solution 7 - Jquery

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);

Solution 8 - Jquery

Try this:

function removeClassDelayed(jqObj, c, to) {    
    setTimeout(function() { jqObj.removeClass(c); }, to);
}
removeClassDelayed($("#div"), "error", 1000);

Solution 9 - Jquery

Try this simple arrow funtion:

setTimeout( () => { $("#div").addClass("error") }, 900 );

Solution 10 - Jquery

Another way...

$("#div").addClass("error"); 
setTimeout(function () { $("#div").removeClass("error"); }, 1000);

Solution 11 - Jquery

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

Thanks me later.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsergView Question on Stackoverflow
Solution 1 - JqueryPetersenDidItView Answer on Stackoverflow
Solution 2 - JqueryJasperView Answer on Stackoverflow
Solution 3 - JquerySandboxView Answer on Stackoverflow
Solution 4 - JquerywarpdesignView Answer on Stackoverflow
Solution 5 - Jqueryuser6322596View Answer on Stackoverflow
Solution 6 - JqueryprodigitalsonView Answer on Stackoverflow
Solution 7 - JqueryAlex JoligView Answer on Stackoverflow
Solution 8 - JqueryPablo MartinezView Answer on Stackoverflow
Solution 9 - Jquerycsandreas1View Answer on Stackoverflow
Solution 10 - JquerymocnikchainView Answer on Stackoverflow
Solution 11 - JqueryShabbir VaghelaView Answer on Stackoverflow