Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  127] [ 2]  / answers: 1 / hits: 34026  / 12 Years ago, tue, november 13, 2012, 12:00:00

EDIT**



I have this click event



$('.next-question').click(function () {

$('td').removeClass('highlight-problem');
var r = rndWord;
while (r == rndWord) {
rndWord = Math.floor(Math.random() * (listOfWords.length));
}
$('td[data-word=' + listOfWords[rndWord].name + ']').addClass('highlight-problem');
$('td[data-word=' + word + ']').removeClass('wrong-letter').removeClass('wrong-word').removeClass('right-letter');
var spellSpace = $('td[data-word=' + listOfWords[rndWord].name + ']').hasClass('right-word');
if (spellSpace) {

$('.next-question').trigger('click');

} else {

$(#hintSound).attr('src', listOfWords[rndWord].audio);
hintSound.play();
$(#hintPic).attr('src', listOfWords[rndWord].pic);
$('#hintPic').show();
$('#hintPicTitle').attr('title', listOfWords[rndWord].hint);
$('#hintPicTitle').show();

}

});


When debug in the console it says too much recursion meaning it is in some sort of endless loop at this point. I think it is because of the trigger(click) event in the if statement, because I seen something similar online.



Basically, I want to say, if given word has the class right-word then move on (hence the trigger), else ...



Is there another way to write it that will not crash?



Here is a fiddle: http://jsfiddle.net/Dxxmh/112/



INSTRUCTION: Click the letters on the right to spell the highlighted area in the grid (The images to help you spell the words are not available in a fiddle so you have to spell them using the console, by looking up the td's)


More From » jquery

 Answers
40

I would do something like this:



if (spellSpace) {
if(score.right != 4)
$('.next-question').trigger('click');


I see like if(score.right == 4) means the end of game. After it is ended - you have no words (or just have no right words, not sure) at all and that is why it never stops. It just triggers click forever instead of stop doing anything and wait for user to click Restart button.



I guess that condition is not enough. Not sure how number of wrong words is counted and handled. But it should be enough to move forward and build correct condition based on your programm logic. Any recursion you start (and you start it with trigger(click)) must have a stop condition.


[#82022] Sunday, November 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cartersinceren

Total Points: 442
Total Questions: 116
Total Answers: 106

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
;