2013-05-30 42 views
-1

我builiding最近的壽命計數器,我想不出什麼問題在這裏,我的意思是我有2個div的,當你與類的div你進球了「活着」,當你在「死了「你得分。壽命計數器問題

現在我做了這個代碼,就秒工作,但它不工作挺直,我的意思是1,2,3,但它的工作是這樣的:http://jsfiddle.net/4Tby5/

或者視覺代碼:

<!DOCTYPE html> 

<html> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Alive - Dead</title> 
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
     <script> 
      var l = 1; 
      var good = " Excelent!"; 
      var bad = " OH NO!"; 
      $(document).ready(function() { 
       $('#alive').hover(function() { 
        if (l > 100) { 
         window.clearTimeout("tim"); 
        } 
        else { 
         document.getElementById("percent").innerHTML = "Life: " + l + good; 
         l++; 
         var tim = window.setTimeout("count()", 1000); 
        } 
        count(); 
       }); 
      }); 
      $(document).ready(function() { 
       $('#dead').hover(function() { 
        if (l < 0) { 
         window.clearTimeout("tim"); 
        } 
        else { 
         document.getElementById("percent").innerHTML = "Life: " + l + bad; 
         l--; 
         var tim = window.setTimeout("count()", 1000); 
        } 
        count(); 
       }); 
      }); 
     </script> 
     <style> 
      body { 
       background: red; 
      } 
      .dead { 
       cursor: url(thumb-down.cur) 6 6, auto; 
       padding-bottom: 285px; 
      } 
      .alive { 
       background: #32ff0a; 
       height: 300px; 
       margin: -8px; 
       cursor: url(thumb-up.cur) 6 6, auto; 
      } 
     </style> 
    </head> 
    <body> 
     <div class="alive" id="alive"> 
     Stay here to survive! 
     </div> 
     <div class="dead" id="dead"> 
     <br /> 
     Stay away from dead area! 
     </div> 
     <div id="percent"></div> 
    </body> 
</html> 

所以我的問題是如何解決這個得到它1,2,3(含2和3,4 ...更換1次)?

+3

有沒有所謂的 「計數」 功能。這不是你如何清除超時。 – Pointy

+0

哦,大聲笑,我accediently刪除它,並忘記刪除它,但我不知道如何設置在超時現在,如果它不計數(),我應該做功能?如果我點,我怎麼能讓它自動工作沒有按鈕。 – copypaste

+0

請告訴我,如果我理解你的問題是正確的。如果用戶進入不良區域,如果計數器達到100「好」,你想給用戶計數器,然後計數器減少? –

回答

0

這是解決你的問題。 問題在您的代碼......懸停功能,只有當鼠標進入計算方法爲......只要不是鼠標仍然裏面。

http://jsfiddle.net/Vdq39/2/

$(document).ready(function() { 
    var good = " Excelent!"; 
    var bad = " OH NO!"; 
    var tim; 
    var counter = 0; 

    function count() { 
     counter++; 
     document.getElementById("percent").innerHTML = "Life: " + counter + good; 
     if (counter > 100) { 
      window.clearInterval(tim); 
     } 
    } 

    function countDown() { 
     counter--; 
     document.getElementById("percent").innerHTML = "Life: " + counter + bad; 
     if (counter < 0) { 
      window.clearInterval(tim); 
     } 

    } 
    $('#alive').hover(function() { 
     if (counter > 100) { 
      window.clearInterval(tim); 
     } else { 
      tim = window.setInterval(count, 1000); 
     } 
    }, function() { 
     window.clearInterval(tim); 
    }); 

    $('#dead').hover(function() { 

     if (counter < 0) { 
      window.clearInterval(tim); 
     } else { 
      tim = window.setInterval(countDown, 1000); 
     } 

    }, 

    function() { 
     window.clearInterval(tim); 
    }); 
}); 
1

你沒有count功能,使一個

你明確使用變量本身而不是其名稱超時

window.clearTimeout(tim);

也與當前的代碼,你將需要使用全局變量

window.tim = window.setTimeout("count()", 1000);

window.clearTimeout(window.tim);

否則clearTimeout不會看到它。

+0

所以,我從setTimeout的刪除count()函數? – copypaste

+0

我刪除了count()函數,仍然沒有工作..它做同樣的事情。 – copypaste

+0

不,你不要刪除settimeout你使計數功能... –