0

I am testing the user to see if they click an element with same id as a randomly selected element. Obviously I want to set a delay between each question - hence the setTimeout. This works fine and every five seconds the user is asked a new question. However, I am trying to change it so that the timeout is fired after the click so that the user has as much time as they want and next question is fired when they click an answer. I tried moving the timeout inside the loop and this caused me problems. I also tried following the logic here, but can't really get it. Any help or advice appreciated.

 function pickRandCounty(){
        if(clickName==askRemoveCounty){
            score++;
        }
        var shuffleCounties = theCountyNames.sort(function(){ return (Math.round(Math.random())-0.5); });
        if (shuffleCounties.length != 0){

        var randCounty = shuffleCounties[Math.floor(Math.random() * regions.length)];
        askRemoveCounty = shuffleCounties.splice(randCounty, 1);
        var showName= document.getElementById('nameDiv');
        showName.innerHTML = "Where's " + askRemoveCounty + "?";
        wasItClicked();
        setTimeout(pickRandCounty, 5000); //WORKING
        }

 function wasItClicked(){
        clickName = null;
        for (var r = 0; r < regions.length; r++) {
            regions[r].click(function(e){
            this.attr({opacity: 0.3});
            clickName = this.data('id');
            this.animate({opacity: 1.0},2000);
            giveFeedback();
        });
        }
        }
7
  • What happens if you move the timeout inside the click function ? Like so: regions[r].click(function(e){ setTimeout(pickRandCounty, 5000); Commented Aug 26, 2015 at 13:12
  • As I said in question, I tried that and it worked in that the next question didn't fire until a click happened, but the array emptied after only a couple of questions and score variable was all over the place - I'm not sure why?! Commented Aug 26, 2015 at 13:21
  • define score outside your method, you have troubling of variable scoping, can you make a fiddle or a plunker? Commented Aug 26, 2015 at 13:22
  • Both of these functions are part of a larger function where score and some other stuff are declared - I think these two functions are the parts that are most relevant to the problem. Commented Aug 26, 2015 at 13:35
  • 1
    @enhzflep Yeah, sure, I get that the process of reproducing the problem may make the issue clearer to me and that it helps others to understand the issue. As a self-learning hobbyist I use stackoverflow to learn and do try to make my questions as clear as possible. I am slow at working things out, so for me to reproduce the problem would be difficult, but I am willing to try. As regards the design flaws, it may be more useful to point them out to me so I can learn from them, rather than just saying they exist. Commented Aug 26, 2015 at 13:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.