Source Source - 5 months ago 11
Javascript Question

delaying a function to execute jquery

I want a button pressed, then the button changes style, and a new function loads, after about 1/2 second of the styling.

$(document).on('click', '.capital_answers', function(){
if(!$('#capital_answers').hasClass('answered')) {
answer = $(this).attr('data-answer');
if(answer==1) {
$(this).addClass('capital_correct');
$('#capital_answers').addClass('answered');
capital_correct()
}
else {
$(this).addClass('capital_wrong');
$('#capital_answers').addClass('answered');
}
delay(500).capital_next();
}
});


So as you can see I am using the delay() method currently, which helps in no way. What options are there in delaying a function in this manner?

Answer

Use setTimeout()

Change:

delay(500).capital_next(); 

To

setTimeout(function(){
   capital_next();
}, 500);

this assumes that capital_next() is a standalone function. It's not clear from code in question

Comments