2013-03-22 87 views
0

我已經使用類似職位的代碼 Random Number Effect In Jquery .. 動畫隨機數,結果http://jsfiddle.net/ZDsMa/94/ ...沒有運行

現在我寫在一個文件中的PHP(index.php)全碼HTML和jQuery和放置在我的服務器...

<html> 
<title>Randomize Number</title> 
<head> 
<style type="text/css"> 
#output { 
    margin: 20px; 
    padding: 20px; 
    background: gray; 
    border-radius: 10px; 
    font-size: 80px; 
    width: 160px; 
    color: red; 
} 
</style> 

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
<script> 
var output, started, duration, desired; 
// Constants 
duration = 5000; 
desired = '50'; 

// Initial setup 
output = $('#output'); 
started = new Date().getTime(); 

// Animate! 
animationTimer = setInterval(function() { 
    // If the value is what we want, stop animating 
    // or if the duration has been exceeded, stop animating 
    if (output.text().trim() === desired || new Date().getTime() - started > duration) { 
     console.log('animating'); 
     // Generate a random string to use for the next animation step 
     output.text('' + Math.floor(Math.random() * 990) 

     ); 

    } else { 
     console.log('animating'); 
     // Generate a random string to use for the next animation step 
     output.text('' + Math.floor(Math.random() * 990) 

     ); 
    } 
}, 1000); 

</script> 
</head> 
<body> 
<div id="output">-</div>      
</body> 
</html> 

的動畫隨機數不顯示(JScript中不運行,只用框 ' - ' characher/CSS)

這有什麼錯我的代碼?

回答

0

您需要包裝,與DOM的$(document).ready()回調交互的代碼:你只需要確保呈現頁面後,你的代碼被執行

$(document).ready(function() { 
    ... 
}) 
+0

我不明白攪拌機,我應該在哪裏放置代碼「$(文件)。就緒()」上面..? – andry 2013-03-22 04:46:45

+0

@andry:http://stackoverflow.com/questions/13062246/when-should-i-use-jquerys-document-ready-function – Blender 2013-03-22 04:48:12

1

。嘗試包裝它:

... 
<script> 
$(function() { 
    var output, started, duration, desired; 
    // Constants 
    duration = 5000; 

    ... 

    }, 1000); 
}); 
</script> 
... 

你可以在這裏找到更多信息jQuery.ready()

+0

thanx @ grenny..now the page work fine .. – andry 2013-03-22 05:06:02

0

在使用中這段JavaScript代碼它的工作

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 
var output, started, duration, desired; 
// Constants 
duration = 5000; 
desired = '50'; 

// Initial setup 
output = $('#output'); 
started = new Date().getTime(); 

// Animate! 
animationTimer = setInterval(function() { 
    // If the value is what we want, stop animating 
    // or if the duration has been exceeded, stop animating 
    if (output.text().trim() === desired || new Date().getTime() - started > duration) { 
     console.log('animating'); 
     // Generate a random string to use for the next animation step 
     output.text('' + Math.floor(Math.random() * 990) 

     ); 

    } else { 
     console.log('animating'); 
     // Generate a random string to use for the next animation step 
     output.text('' + Math.floor(Math.random() * 990) 

     ); 
    } 
}, 1000); 
})