In JQuery, Is it possible to get callback function after setting new css rule?

后端 未结 6 1105
一生所求
一生所求 2020-12-03 17:05

I have $(\'.element\').css(\"color\",\"yellow\") and I need that next event was only after this one, something looks like $(\'.element\').css(\"color\",\"

6条回答
  •  旧巷少年郎
    2020-12-03 17:31

    You can use setTimeout to increase the sleep time between the alert and the css like this:

    function afterCss() {
        alert(1);
    }
    
    $('.element').css("color","yellow");
    setTimeout(afterCss, 1000);
    

    This will make the alert appear 1 second after the css changes were committed.

    This answer is outdated, so you might want to use promises from ES6 like the answer above.


    $('.element').css("color", "yellow").promise().done(function(){
        // The context here is done() and not $('.element'), 
        // be careful when using the "this" variable
        alert(1);
    });
    

提交回复
热议问题