SetTimeout Function not executing code | XM Community
Skip to main content

Hello,
I am trying to delay the amount of time that the answer choice is selected. If a person spends more than, say 3000 milliseconds trying to answer the question, then it will automatically select the first answer choice.
Here's what I have:
setTimeout(function() {
jQuery("#"+this.questionId+" select option:eq(1)").prop("selected",true);
}, 3000);
The code within the function works fine on its own (if it's not in the setTimeout function), but doesn't get executed if it's put in the function once those 3 seconds are up. I also know it's not an issue with the function itself, because when I add an alert in there, the alert goes off after 3 seconds.
More info: that first choice which is automatically selected is hidden from the survey respondent. I used a lot of the code from this post to implement my overall solution. Let me know if I should post more of my code to help with the debugging.
Any help is appreciated, thanks a lot! 😀

this 
refers to the context of the specific function being executed, so
this.questionId
will return and error/null. Either store the value of
this.questionId 
into a variable. Or define
that = this
before the setTimeout method and then use
that.questionId


ahmedA Thank you so much for the explanation, that fixed it!


Leave a Reply