动画随机数不运行
Animated random number not running
我使用了类似post in的代码Jquery中的随机数效果和结果http://jsfiddle.net/ZDsMa/94/…
现在我写完整的代码html和jquery在一个文件php(index.php
)和放置在我的服务器…
<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未运行,只有带有'-'字符的框/css)
我的代码有什么问题?
您只需要确保在页面呈现后执行您的代码。试着用:
...
<script>
$( function () {
var output, started, duration, desired;
// Constants
duration = 5000;
...
}, 1000);
});
</script>
...
你可以在这里找到更多信息jQuery.ready().
您需要在$(document).ready()
回调中包装与DOM交互的代码:
$(document).ready(function() {
...
})
我使用这个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);
})
相关文章:
- 如何使用phaser使html5游戏在移动设备浏览器上运行
- 使用压缩的JavaScript文件(不是运行时压缩)
- Javascript运行php文件,然后下载文件
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- Angularjs代码未在匿名函数中运行
- jquery设置为使用参数运行
- 如何根据时间运行不同的脚本
- 如何在javascript中获得与特定数字相等的随机数
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 将文本框链接到由按钮运行的javascript公式
- 为什么不是't运行此Javascript的Chrome
- ng应用程序使脚本无限运行
- 如何在运行时在angular 2中加载外部js脚本
- Javascript动态变量(添加了随机数)
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 当我的函数在javascript中运行时,我的随机数生成器不断变化
- 运行一个随机数生成器在按钮按下
- Javascript新的随机数每次函数运行
- 动画随机数不运行