打字机 时间随机效果

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload=function()
{
var oBtn=document.getElementById('btn');
var oBox=document.getElementById('box');
var oTex=document.getElementById('tex');
oBtn.onclick=function()
{
var value=oTex.value;
if(!value)
{
alert('请输入内容');
return;
};
var n=0;
setTimeout(f,rnd(60,500));

function f(){

n++;
oBox.innerHTML= value.substring(0,n)+'<span>_</span>';
if(n==value.length)
{

oBox.innerHTML= value.substring(0,n);
alert('关');
}
else
{
setTimeout(f,rnd(60,500));
};
};

};
};

function rnd(n,m)
{
return parseInt(Math.random()*(m-n+1)+n);
};
</script>

</head>

<body>
<input type="button" value="aa" id="btn">
<textarea id="tex"></textarea>
<div id="box" style="width:500px;border:1px solid #000;height:500px;"></div>
</body>
</html>

 

 

posted on 2014-08-27 16:48  moliwanzi  阅读(118)  评论(0编辑  收藏  举报

导航