开发者社区 问答 正文

定时器时间清零!!

我想在屏幕滚动的时候把_time设为零,就是立马让div为hide(),而不是关闭定时器
这样能做吗?

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title></title>
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
</head>
<body>
      <input type="button" value="点击" class="show"/>  
    <div class="dis">hello</div>  
    <script>  
     $(function(){
           var _time = 3000;
              $('.show').click(function(){
                  timer3 = setTimeout(function(){
                            $(".dis").hide();                    
                    },_time);    
              })
              
              window.scroll(function(){
                                          
              })
     })
            
    </script>  
</body>

展开
收起
小旋风柴进 2016-05-31 09:28:06 2271 分享 版权
1 条回答
写回答
取消 提交回答
  • clear掉定时器就行了,然后$(".dis").hide();

    $(function(){
       var _time = 3000;
        var timer3 = null;
          $('.show').click(function(){
              timer3 = setTimeout(function(){
                        $(".dis").hide();                    
                },_time);    
          })
          
          window.scroll(function(){
                clearTimeout(timer3);  
                $(".dis").hide();                  
          })
    2019-07-17 19:21:18
    赞同 展开评论
问答地址: