
<body>
<div id="t" >10</div>
<script>
var i=10
window.setInterval(function() {
(i++)
document.getElementById('t').innerHTML=10+(i-10)*2
}, 3000)
</script>
<p>每3秒自动增加数字2</p>
</body>
效果是从10的显示开始每3秒自动增加2。
3000和*2那里可以随便改。
似乎不太好解决,B数据中需存下当前时间.<html>
<head>
<title>JS OBJECT STUDY</title>
<script>
//保持数组长度为10,当放第十一个元素的时候,原数组中的最老元素将被取代
function MyArray(){
this.array=new Array()//存放元素
this.pushTime=new Array()//存放时间
}
MyArray.prototype.push=function(obj){
var ctime=new Date().getTime()
if(this.array.length>=10){
//拷贝数组
var pt=this.pushTime.concat("")
//对数组排序
pt.sort()
var index=0
//寻找最老元素的下标
for(var i=0i<this.pushTime.lengthi++){
if(this.pushTime[i]==pt[0]){
index=i
break
}
}
//将最老元素替换
this.array.splice(index,1,obj)
this.pushTime.splice(index,1,ctime)
}else{
this.array.push(obj)
this.pushTime.push(ctime)
}
}
MyArray.prototype.pop=function(){
this.pushTime.pop()
return this.array.pop()
}
var a=new MyArray()
a.push("I")
a.push("love")
a.push("you")
a.push(",")
a.push("gao")
a.push("yuan")
a.push("hong")
a.push("ceng")
a.push("jing")
a.push("!")
a.push("ly")
alert(a.array)
alert(a.pop())
</script>
</head>
<body>
</body>
</html>
在后台部署定时任务吧,前台计时器不实际:
页面刷新意味着重新计时(计时器被重置)
没有人会在一个页面停留那么长时间(2小时)
用计时器显得有点……,当然了,一定要前台使用JQuery的话,部署一个函数来计算就好:
加载的时候读取数值基数,然后根据当前时间直接计算出最终变动的值,最后显示即可。
最好的做法还是后台处理,前台请求的数据就是变动后的数值(不管是谁,何时请求?结果都是一样的)。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)