Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动画随机数未运行_Javascript_Jquery_Html - Fatal编程技术网

Javascript 动画随机数未运行

Javascript 动画随机数未运行,javascript,jquery,html,Javascript,Jquery,Html,我使用了类似post的代码 .. 结果 现在我在一个文件php(index.php)中编写完整的html和jquery代码,并将它们放在我的服务器上 <html> <title>Randomize Number</title> <head> <style type="text/css"> #output { margin: 20px; padding: 20px; background: gray; bo

我使用了类似post的代码 .. 结果

现在我在一个文件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>

随机数
#输出{
利润率:20px;
填充:20px;
背景:灰色;
边界半径:10px;
字号:80px;
宽度:160px;
颜色:红色;
}
var输出、开始、持续时间、期望值;
//常数
持续时间=5000;
期望值='50';
//初始设置
输出=$(“#输出”);
started=新日期().getTime();
//动画!
animationTimer=setInterval(函数(){
//如果该值是我们想要的,请停止设置动画
//或者,如果已超过持续时间,请停止设置动画
if(output.text().trim()==所需的| |新日期().getTime()-started>duration){
console.log('animating');
//生成用于下一动画步骤的随机字符串
output.text(“”+Math.floor(Math.random()*990)
);
}否则{
console.log('animating');
//生成用于下一动画步骤的随机字符串
output.text(“”+Math.floor(Math.random()*990)
);
}
}, 1000);
-                    
未显示动画随机数(jscript未运行,仅带有“-”字符/css的框)


我的代码怎么了?

您需要将与DOM交互的代码包装在
$(文档)中。ready()
回调:

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

您只需要确保在呈现页面后执行代码。尝试用以下内容将其包装:

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

    ...

    }, 1000);
});
</script>
...
。。。
$(函数(){
var输出、开始、持续时间、期望值;
//常数
持续时间=5000;
...
}, 1000);
});
...

您可以在这里找到更多信息。

您好,请使用此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);
})

$(文档).ready(函数(){
var输出、开始、持续时间、期望值;
//常数
持续时间=5000;
期望值='50';
//初始设置
输出=$(“#输出”);
started=新日期().getTime();
//动画!
animationTimer=setInterval(函数(){
//如果该值是我们想要的,请停止设置动画
//或者,如果已超过持续时间,请停止设置动画
if(output.text().trim()==所需的| |新日期().getTime()-started>duration){
console.log('animating');
//生成用于下一动画步骤的随机字符串
output.text(“”+Math.floor(Math.random()*990)
);
}否则{
console.log('animating');
//生成用于下一动画步骤的随机字符串
output.text(“”+Math.floor(Math.random()*990)
);
}
}, 1000);
})

我不懂Blender,我应该把代码“$(document).ready()”放在哪里..???@andry: