2016-04-22 45 views
1

如果有人帮助我,我将非常感激。我是JS中的新手。我尝试多次调用函数作为我的全局变量。我的问题是该函数同时执行。如何使它交替执行?这里是我的代码:如何在JS中交替调用一个函数?

 #general{width:400px;height:400px;margin:0 auto;} 
     .cell{width:50%;height:50%;color:black} 
     .cell:nth-of-type(1){float:left;background: #81E058} 
     .cell:nth-of-type(2){float:right;background: red} 
     .cell:nth-of-type(3){float:left;background: blue} 
     .cell:nth-of-type(4){float:right;background: yellow} 



<div id='generalwrapper'> 
    <div id='general'>     
     <div class='cell' onclick='simon(this.id)'></div> 
     <div class='cell' onclick='simon(this.id)'></div>   
     <div class='cell' onclick='simon(this.id)'></div> 
     <div class='cell' onclick='simon(this.id)'></div>      
</div> 
<div id='buttondiv'> 
    <button onclick='startGame()'>On</button> 
</div> 
</div> 

     var browser=[]; 
     var count=1; 

     function startGame(){ 
      for(var i=0;i<count;i++){ 
       browserturn(); 
      } 
      count++; 
      } 

     function browserturn(){ 
      var x=getNumber(); 
      var element=x.toString(); 
      browser.push(element); 
      var y=document.getElementById(element); 
      y.click(); 
      $(y).delay(100).fadeOut().fadeIn('slow'); 
     } 


     function getNumber(){ 
      var randomNumber=Math.floor((Math.random() * 4)+1); 
      return randomNumber; 
     } 
+0

你所说的“同时执行”和“交替”是什么意思?你的意思是说fadeout和fadeIn是同时发生多个呼叫吗? – nurdyguy

+0

如果你试图让它闪烁(淡出然后回来)与数字相同的次数,那么你需要把它改成递归循环而不是正常的循环。那是你正在尝试的吗? – nurdyguy

+0

是的,divs不是一个接一个地改变颜色,而是一起改变颜色。 – Armine

回答

0

您应该使用setTimeout延迟一个recursive函数调用。我已经写了下面一个基本的递归函数:

var myDiv = document.getElementById('myDiv'); 
 
var timesToRepeat = 4; 
 

 

 
function myRecursiveFunction(count) { 
 
    myDiv.innerText += " ..." + count; 
 
    count++; //increment count 
 
//this is our exit condition -- otherwise it recurs infinitely!! 
 
    if (count <= timesToRepeat) { 
 
    setTimeout(function() { 
 
//after 600ms, call our function again 
 
     myRecursiveFunction(count); 
 
    }, 600) 
 
    } 
 
} 
 
//our initial call to the function 
 
myRecursiveFunction(1);
<div id="myDiv"> 
 

 
</div>

+0

你好。我试图使用你的代码,但它在我的情况下不起作用,尽管我看到它完全运行在这里。我的代码有什么问题?你能帮我吗?这是我的按钮来调用函数。功能startGame(计数){ 计数++; 如果(计数<=计数器){ 计数++; 的setTimeout(函数(){ startGame(计数); },600) \t \t} \t limit ++; } – Armine

+0

很难说,只是这个片段...你可以创建一个小提琴吗? https://jsfiddle.net/ –

+0

我已经解决了这个问题,非常感谢你的帮助。:) – Armine

1

您需要的时间间隔:

var interval; 
var limit = 1; 
var count = 0; 

更换startGame此:

function startGame() 
    { 
      interval = setInterval(browserturn, 1000); 
      limit++; 
    } 

间隔调用你的函数withi的1000毫秒的时间间隔,并释放控制到渲染器,所以你会看到变化

在browserturn你可以增加耳鼻喉科柜台

 function browserturn(){ 
     var x=getNumber(); 
     var element=x.toString(); 
     browser.push(element); 
     var y=document.getElementById(element); 
     y.click(); 
     $(y).delay(100).fadeOut().fadeIn('slow'); 

     // -------------------------------------- 
     counter++; 
     if(counter < limit) 
     { 
      clearInterval(interval); 
      count = 0; 
     } 
     // -------------------------------------- 
    } 

,并清除间隔,如果计数>限制

+0

你的代码不会同时运行,但只有它可以让你看到更改,直到它结束 –

+0

感谢您的帮助,我使用了更改,但现在变化无止境。我需要的是1更改通过第一次点击,2更改通过第二次点击等等。 – Armine

+0

你需要的所有点击一次初始化改变计数器= 0首次 –

相关问题