<script type="text/javascript">
$(function(){
window.setTimeout('shijian()',1000); })
function   shijian(){
  var shuju1 = $("#shuju1").val();
  var shuju2 = $("#shuju2").val();  var hou1=parseInt(shuju1);
  var hou2=parseInt(shuju2);
  var cha=hou2-hou1; 
  var i=0
while (i<cha)
{
        hou1=hou1+1;我想要的效果是当hou1这个字段的值为501的时候,id为shuju1的文本框显示为501停顿两秒中之后,在执行i=i+1这个操作
$("#shuju1").attr("value",hou1);
i=i+1;
}
}
</script>
</head><body >
 <form name=form1>
<input  type="text" id="shuju1" value="500"/>
<input  type="text" id="shuju2" value="510"/>
</form>
</body>
谁能告诉我   红字部分的怎么实现啊  谢谢

解决方案 »

  1.   

    是这样的效果吗<html><head>
    <script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function(){
    window.setTimeout('shijian()',1000);  })var i = 0;
    function shijian(){
      var shuju1 = $("#shuju1").val();
      var shuju2 = $("#shuju2").val();  var hou1=parseInt(shuju1);
      var hou2=parseInt(shuju2);
      var cha=hou2-hou1;  
      
    if (i<cha)
    {
      hou1=hou1+1;//我想要的效果是当hou1这个字段的值为501的时候,id为shuju1的文本框显示为501停顿两秒中之后,在执行i=i+1这个操作
      $("#shuju1").attr("value", hou1);  window.setTimeout('shijian()', 2000);  
    }}
    </script>
    </head><body >
     <form name=form1>
    <input type="text" id="shuju1" value="500"/>
    <input type="text" id="shuju2" value="510"/>
    </form>
    </body></html> 
      

  2.   

    lz的代码中是while循环 和if不一样的 
    楼上这么写 循环和setTimeout是2个线程在跑 循环是有问题的
      

  3.   

    规定时间要用 setInterval函数啊
      

  4.   

    谢谢楼上三位的支持与帮助,解决了<form name="form1">
        <input type="text" id="shuju1" value="500" />
        <input type="text" id="shuju2" value="1000" />
    </form>
    <script type="text/javascript">
    $(function(){
    time_seting();
    })var shuju1 = parseInt($("#shuju1").val());
    var shuju2 = parseInt($("#shuju2").val());
    var timeset = '';function time_seting(){
    clearInterval(timeset);
    timeset = window.setInterval(shijian,5);
    }function shijian(){
    var len = shuju2 - shuju1;
    $("#shuju1").val(shuju1);
    shuju1=shuju1+1;
    if(shuju2-shuju1<1){
        shuju1=shuju1+(shuju2-shuju1);
    }
    if(len<=0 ){
    clearInterval(timeset);
    //alert("结束");
    }else{
    time_seting();
    }
    }
    </script>