2013-10-03 73 views
0

我只是有幾個問題,一個反應測試,我做:後jQuery的反應測試延遲問題

  • 好吧,首先,我想有一個隨機的停頓(2-5秒)玩家點擊一個div。

  • 其次,我想讓div總共出現5次,所以玩家可以獲得5次嘗試。

對於第一個問題,我嘗試使用setTimeout函數。我試圖通過使用'for'循環來限制div出現的次數來解決第二個問題。

For example: 

    for(var i = 1; i < 5; i++) { 
     $div.css({ 
      left: Math.floor(Math.random() * widthMax), 
      top: Math.floor(Math.random() * heightMax) 
     }); 
    } 

但是,我無法解決這些問題。

您可以在這裏嘗試一下: http://jsfiddle.net/tghca/7/

任何幫助將不勝感激!謝謝!

+0

的延遲,如果是爲了什麼?點擊重新定位div後?或啓用下一個點擊 –

+0

您能否請添加具有div屬性(可見性)的事件的特定列表?我目前不明白div何時出現和消失。 –

+0

像http://jsfiddle.net/arunpjohny/un9b9/1/ –

回答

1

喜歡的東西

$('div').hide(); 

$('.start').click(function() { 
    $(this).hide(); 
    $('.hint').hide(); 
    $('div').show(); 
    makeDiv(); 
}); 

var counter = 0; 

function testClick() { 
    var docHeight = $(document).height(), 
     docWidth = $(document).width(), 
     $div = $('#test'), 
     divWidth = $div.width(), 
     divHeight = $div.height(), 
     heightMax = docHeight - divHeight, 
     widthMax = docWidth - divWidth; 

    $div.hide(); 
    setTimeout(function() { 
     $div.css({ 
      left: Math.floor(Math.random() * widthMax), 
      top: Math.floor(Math.random() * heightMax) 
     }).show(); 
     counter++; 
     if (counter < 5) { 
      makeDiv(); 
     } 
    }, Math.floor(Math.random() * 3000) + 2000) 
} 

function makeDiv() { 
    $('#test').one('click', testClick); 
} 

演示:Fiddle

+0

是的!而已!非常感謝你,阿倫! – user2840892

+0

@ user2840892然後你會介意將答案標記爲已接受 –