完整例子<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> </head> <body> <div id="show"></div> <script type="text/javascript"> var num = 0; var et = setInterval(function(){ num++; if(num>100){ clearInterval(et); }else{ document.getElementById('show').innerHTML = num; } },100) </script> </body> </html>
window.setInterval("test()",1000);//每秒调用一次test()方法
至于你在test 方法里面做不做ajax就是你的事了
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<script>
window.onload = function(){
window.setInterval("test()",1000);
}
var i = 0;
function test(){
i++;
alert(i);
}
</script>
</head>
<body></body>
</html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<script>
window.onload = function(){
window.setInterval("test()",1000);
}
var i = 0;
function test(){
if(i>=0 && i<=100)
{
i++;
alert(i);
}
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<script>
window.onload = function(){
window.setInterval("test()",1000);
}
var i = 0;
function test(){
if(i>=0 && i<100)
{
i++;
alert(i);
}
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<title> New Document </title>
</head> <body>
<div id="show"></div>
<script type="text/javascript">
var num = 0;
var et = setInterval(function(){
num++;
if(num>100){
clearInterval(et);
}else{
document.getElementById('show').innerHTML = num;
}
},100)
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Refresh" content="1"/>
<title>hehe</title>
<style>
.count{
border:1px solid #7e7474;
width:500px;
height:500px;
margin:0px auto;
text-align:center;
line-height:500px;
background:#f0f0f0;
font-size:48px;
}
#math{font-size:42px;text-shadow: 0 -1px 0 #72a441;color:#360;font-weight:700;}
</style>
<script src="./jquery-1.8.3.min.js" type="text/javascript"></script>
<script>
function magic_number(value) {
var num = $("#math");
num.animate({count: value}, { //从一个数字到另一个数字变换
duration: 500,
step: function() {
num.text(String(parseInt(this.count)));
}
});
};
function update() { //向后台发送ajax请求
$.getJSON("mathnum.php?jsonp=?", function(data) {
magic_number(data.n);
});
};
setInterval(update, 1000); //执行1秒刷新一次
update();
</script>
</head>
<body>
<div class="count" ><span id="math"></span></div>
</body>
</html><?php
$total_data = array(
'n' => rand(0,999) //随机数
);
echo $_GET['jsonp'].'('. json_encode($total_data) . ')';
echo "<hr/>";
?>
上面的代码输出是随机的数,我想更改为数字动态累加,
就是输出从1开始++,然后是2,3,……在同位置到另一个数的变换循环,
哪里需要更改的地方,大神…
if($_POST) {
echo $_POST['last']+1; //将传入的数字加一后返回
exit;
}
?>
<script src="scripts/jquery-1.8.3.min.js"></script>
<script>
$(function() {
function update() { //向后台发送ajax请求
$('#math').load(location.href, { last : $('#math').html()});
};
setInterval(update, 1000); //执行1秒刷新一次
update();
});
</script>
<span id="math"></span>
总要按你的改改吧