题目要求:在网页中随机地显示五个div,用setTimeout控制,时间范围是0.5s~1s之间,当第五个div生成后,出现一个警告框。
用JavaScript实现
请各位高人予以指点。

解决方案 »

  1.   

    <script type="text/javascript">
    var i=0;
    function init(){
    var a=parseInt(500+Math.random()*500);
    var b=document.createElement("div");
    b.style.backgroundColor="red";
    b.style.width="100px";
    b.style.height="100px";
    b.innerHTML=i+"";
    document.body.appendChild(b);
    i++;
    p=window.setTimeout("init()",a);
    if(i==5){
    stops();
    alert("");
    }
    }
    function stops(){
    window.clearTimeout(p);
    }
    window.onload=init;
    </script>这样试试