先说下功能,我是希望能在页面上循环显示1到4的数字,以每刻1秒钟更新一次程序如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function Photo()
{
this.currItem = 0;
this.max = 4;
this.play = loop;
}
function loop()
{
document.write(this.currItem);
this.currItem++;
if(this.currItem >= this.max)
this.currItem = 0;
setTimeout(loop,1000);
}
//-->
</SCRIPT>
</HEAD> <BODY>
<script>
var photo = new Photo();
photo.play();
</script>
</BODY>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function Photo()
{
this.currItem = 0;
this.max = 4;
this.play = loop;
}
function loop()
{
document.write(this.currItem);
this.currItem++;
if(this.currItem >= this.max)
this.currItem = 0;
setTimeout(loop,1000);
}
//-->
</SCRIPT>
</HEAD> <BODY>
<script>
var photo = new Photo();
photo.play();
</script>
</BODY>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
var tid;
function Photo()
{
this.currItem = 0;
this.max = 4;
this.play = (function(obj){
return function(){
clearTimeout(tid);
document.getElementById("pad").innerHTML=obj.currItem;
obj.currItem++;
if(obj.currItem >= obj.max) obj.currItem = 0;
tid = setTimeout(obj.play,1000);
}
})(this);}
//-->
</SCRIPT>
</HEAD> <BODY>
<div id="pad"></div>
<script>
var photo = new Photo();
photo.play();
</script>
</BODY>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function Photo()
{
this.currItem = 0;
this.max = 4; }
Photo.prototype.play=function()
{ document.write(this.currItem);
this.currItem++;
if(this.currItem >= this.max) {
this.currItem = 0;
return
}
setTimeout(this.play.call(this),1000);
}
//-->
</SCRIPT>
</HEAD> <BODY>
<script>
var photo = new Photo();
photo.play();
</script>
</BODY>
</HTML>
我是这么做的
function Photo()
{
this.currItem = 0;
this.max = 4;
this.play = loop(this);
}
function loop(obj)
{
clearTimeout(tid);
document.getElementById("pad").innerHTML=obj.currItem;
obj.currItem++;
if(obj.currItem>=obj.max)
obj.currItem = 0;
tid=setTimeout(obj.play,1000);
return obj;
}
但运行后程序指着这句报错tid=setTimeout(obj.play,1000);
该怎么改?
var i = 1;
var something = document.getElementById('somethis').innerHTML;
function loops() {
something = '' + (i++);
setTimeout(arguments.callee, 1000);
}
loops();
})();
<script>
function printit(i){
var a=document.getElementById('a'), b=i==s.length;
a.innerHTML=!b?s.charAt(i) : 1; i=b?0:i
var id=window.setTimeout("printit("+i+"+1)",1000)
}window.onload=function(){ s="1234"; printit(0) ;}</script>
<body>
<div id='a' style="color:red;font-size:500%"></div>
</body>