100分求助高难问题:如何实现动态显示x/y的值?
问题描述如下:
从页面文本框中输入一个整数,如54685,设为X;
当前逝去时间为Y,以秒为单位;
点击“开始”按钮之后,“开始”变为“停止”按钮,随着Y的增长,页面动态显示X/Y的值;
点击“停止”按钮后,“停止”变为“开始”按钮,页面静态显示此时的X/Y最终值。
------解决方案-------------------- <input type= "text " name= "X " id= "X " /> <br>
<span id= "result "> </span> <br>
<button onclick= "doAction(this); "> 开始 </button>
<script>
function doAction(currObj){
if(currObj.value == "开始 "){
window.stop = false;
currObj.value = "结束 ";
var X = parseInt(document.getElementById( "X ").value);
var Y = 1;
var result = document.getElementById( "result ");
setTimeout(function(){
if(window.stop) return;
result.innerText = X / (Y++);
setTimeout(arguments.callee, 1000);
}, 1000);
}else{
window.stop = true;
currObj.value = "开始 ";
}
}
</script>
------解决方案-------------------- <script language= "javascript ">
var x;
var y=0;
var stl;
function Tostart(){
x=document.form1.x.value;
y++;
resault=x/y;
document.getElementById( "outNum ").innerText=resault;
document.getElementById( "y ").innerText=y;
}
function Tostop(){
window.clearTimeout(startTimout);
}
function num(){
if(document.form1.btn.value== " 开 始 "){
stl=window.setInterval( "Tostart() ",1000);
document.form1.btn.value= " 停 止 ";
}else{
//y=0;//取消注释则每次重新计时
window.clearInterval(stl);
document.form1.btn.value= " 开 始 ";
}
}
</script>
<form name= "form1 " method= "post " action= " ">
<div> X: <input type= "text " name= "x "> </div>
<div> y: <span id= "y "> </span> </div>
<div> resault: <span id= "outNum "> </span> </div>
<div> <input type= "button " name= "btn " value= " 开 始 " onClick= "num(); "> </div>
</form>
------解决方案-------------------- <html>
<head>
<script language= "JavaScript ">
<!--
var timer = null;
var isRun = false;
function begin(btn){
if(isRun){ clearInterval(timer); btn.value= "开始 ";isRun = false; return;}
var sou = document.getElementById( "txtSou ");
var out = document.getElementById( "txtOut ");
var val = parseInt(sou.value,10);
var i=0;
var fun = function (){ out.value = (val--)+ '/ '+(i++); btn.value= '停止 ';}
timer = setInterval(fun,100);
isRun = true;
}
//-->
</script>
</head>
<body>
<input type= "text " id= "txtSou " size= "150 " value= "654321 "> <br>
<input type= "button " value= "开始 " onclick= "begin(this) "> <br>
<input type= "text " id= "txtOut " size= "150 ">
</body>
</html>
------解决方案-------------------- <meta http-equiv= "Content-Type " content= "text/html; charse