2016-06-09 48 views
0

我想運行一個for循環,但我不希望它一次全部執行。我需要暫停執行代碼,直到用戶點擊2次網頁

for(var i = 0; i < array1.length;i++){ 
    var tempArray[]; 
    var tempArrayRevised = []; 
confirm("Click the page twice!"); 
    //This is where I want the code to pause until the webpage has been clicked on twice 

    for(var i = 0; i < 2; i++){ 
     tempArrayRevised.push(tempArray[i+1]); 
    } 
    array2.push(tempArrayRevised); 
    //unhighlight position 
} 
+0

我想你應該提供更多的上下文。你爲什麼做這個?爲什麼不只是聽取2個點擊事件,然後在發生時進行循環播放? – Jbird

+0

如果目標是循環遍歷2個部分的數組,然後使用Array.protototype.slice()創建兩個數組。 – Jbird

回答

0
runned=false; 
window.addEventListener("dblclick", function(){ 
if(runned==true){ 
    for(var i = 0; i < 2; i++){ 
tempArrayRevised.push(tempArray[i+1]); } 
array2.push(tempArrayRevised);}} 

for(var i = 0; i < array1.length;i++){ 
var tempArray[]; 
var tempArrayRevised = []; 
confirm("Click the page twice!"); 
runned=true; 
} 

像這樣的事情?

0

這將是這樣的:

for (var i = 0; i < array1.length; i++) { 
    var tempArray[]; 
    var tempArrayRevised = []; 
    var pageClickCount = 0; 

    if (confirm("Click the page twice!")) { 
     document.body.addEventListener('click', function() { 
      pageClickCount += 1; 
      execRest(); 
     }); 
     document.body.addEventListener('dblclick', function() { 
      pageClickCount += 2; 
      execRest(); 
     }); 
    } 

    function execRest() { 
     if (pageClickCount >= 2) { 
      for (var i = 0; i < 2; i++) { 
       tempArrayRevised.push(tempArray[i + 1]); 
      } 
      array2.push(tempArrayRevised); 
      //unhighlight position 
     } 
    } 
} 
+0

您正在重新聲明您的數組,每個循環單擊count和execRest函數。就是不行。 – Jbird

+0

我需要它們每次循環時重置。我真的需要該程序彈出一個確認,要求他們點擊兩次,然後等待他們點擊兩次,然後其餘部分通過 – Victoria

+0

循環即使您需要變量每次「重置」(在這種情況下,您可能不會根本不需要循環)你不會重新聲明它們,就像在這個例子中看到的一樣,你肯定不會在循環中重新聲明一個函數。 – Jbird

0

我不是很確定你爲什麼這樣做,但根據所提供的信息,這裏是幾行代碼是:

  1. 開始遍歷「thingArray」和增量「loopCount」變種
  2. 當「loopCount」等於2,我們跳出循環並綁定click事件
  3. 當點擊事件已觸發TWIC e,我們稱之爲「簡歷」功能,循環使用剩餘的項目。

變量loopCount並不是必需的,但是我已經把它放在那裏,以便您可以參考它來創建在此過程中發生的其他邏輯。你顯然可以使用循環內的本地i var來進行簡單的增量檢查。

'break'語句是這裏的關鍵組件,代碼主要是爲了證明這一點。

小提琴:https://jsfiddle.net/jbird/2zza826j/

在瀏覽器控制檯,你會看到一條消息,每次點擊的事件,最後,當循環中的「恢復」功能完成後,您會看到整數4這是長的陣列顯然。

哦,我不會建議直接在窗口上綁定用戶啓動的事件。

var clickTarget = document.querySelectorAll('.click-target')[0], 
    loopCount = 0, 
    clickCount = 0, 
    thingArray = ['thing1', 'thing2', 'thing3', 'thing4'], 
    thingDoneArray = []; 

function start() { 
    for (var i = 0; i < thingArray.length; i++) { 
     loopCount += 1; 

     thingDoneArray.push(thingArray[i]) 

     if(loopCount == 2) { 
      // Bind your click event 
      clickTarget.addEventListener('click', onClick); 

      // Alert the user 
      alert('Click the page twice...for some reason.'); 

      // Break out of the loop (this is the important part) 
      break; 
     } 
    } 
} 

function resume() { 
    for(var i = 0; i < thingArray.length - thingDoneArray.length; i++) { 
     loopCount += 1; 
    } 

    console.log(loopCount); 
} 

function onClick(evt) { 
     evt.preventDefault(); 

    clickCount += 1; 

    console.log('1 click event happened.'); 

    if(clickCount == 2) { 
     clickTarget.removeEventListener('click', onClick); 
     resume(); 
    } 
} 

start();