Math.random()后的延迟问题...

keith26 2009-04-14 09:35:33
代码很简单,如下.


// JavaScript Document
window.onload=initLink;

var adImages=new Array("img2.jpg","img22a.jpg","img22b.jpg");
var adLinks=new Array("google.com","baidu.com","gougou.com");


function initLink()
{

var imgParent=document.getElementById("img2").parentNode;
if(imgParent.tagName=="A")
{
imgParent.onclick=NewLocation;
}

nextRotate();
}

function nextRotate()
{

var adNum=Math.floor((Math.random()*adImages.length));
var img=document.getElementById("img2");
img.src=adImages[adNum];
setTimeout("nextRotate()",1000);
}


function NewLocation()
{
document.location.href="http://www."+adLinks[0];
return false;
}




<a href="texthtml.html"><img src="img2.jpg" alt="img2" id="img2"/></a>



我想实现的功能很简单,就是要图片随机的切换而已..但当运行代码以后发现图片切换几次以后会有一个延迟(超过1秒).请问是什么原因...
我用chrome测试代码的..
...全文
105 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
keith26 2009-04-15
  • 打赏
  • 举报
回复
伪随机数会重复这个问题我知道..我是不明白为什么它会有延迟的呢..
keith26 2009-04-15
  • 打赏
  • 举报
回复
哦..原来是这样..感谢大哥了..
呵呵.小弟学习了..
结贴了.
caiying2009 2009-04-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 keith26 的回复:]
伪随机数会重复这个问题我知道..我是不明白为什么它会有延迟的呢..
[/Quote]
请看#1的实例,随机数重复时,图片不变,就“延迟”了^_^
caiying2009 2009-04-15
  • 打赏
  • 举报
回复
随机数重复了^_^

<script language="javascript">
<!--
// JavaScript Document
window.onload=initLink;

var adImages=new Array("images/1.jpg","images/2.jpg","images/3.jpg");
var adLinks=new Array("google.com","baidu.com","gougou.com");
function initLink(){
var imgParent=document.getElementById("img2").parentNode;
if(imgParent.tagName=="A"){
imgParent.onclick=NewLocation;
}
nextRotate();
}
var ch=0
function nextRotate(){
var adNum=Math.floor((Math.random()*adImages.length));
var img=document.getElementById("img2");
img.src=adImages[adNum];
setTimeout("nextRotate()",1000);
ch++
document.getElementById("ttt").innerHTML=ch+":"+adNum
}


function NewLocation(){
document.location.href="http://www."+adLinks[0];
return false;
}
//-->
</script>
<a href="texthtml.html"><img src="img2.jpg" alt="img2" id="img2"/></a>
<br>
<span id=ttt></span>




random()--伪随机,有相邻重复

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧