我正在测试用户,看他们是否点击了一个与随机选择的元素具有相同ID的元素。显然我想在每个问题之间设置延迟 - 因此是setTimeout。这工作正常,每五秒钟就会向用户询问一个新问题。但是,我正在尝试更改它,以便在单击后触发超时,以便用户有他们想要的时间,并在单击答案时触发下一个问题。我尝试在循环内移动超时,这引起了我的问题。我也试过遵循逻辑here,但不能真正得到它。任何帮助或建议表示赞赏。
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();
});
}
}