English 中文(简体)
停止按键单击后不工作
原标题:clearInterval() not working after stop button click
  • 时间:2012-05-25 15:57:55
  •  标签:
  • html
  • jquery

我正试图使用 java 脚本刷新 div 。 设置 Interval () 和 clear Interval (), 工作正常, 但我想在单击停按钮时停止单 div 的刷新进程 。 清除 Interval 无效 。 @ clear Interval not working herer

<script type ="text/javascript">
        $(document).ready(function () {
            $( .output ).click(function () {
                var id = $(this).closest( .g ).attr( id );

                Go(id);

            })
            $( .bt ).click(function () {
           var id = $(this).closest( .g ).attr( id );
                stop(id)
            });

            function Go(id) {
                id = setInterval(function () {
                    Chat_msg( Ch04 , id, u)
                }, 3000);
            };
            function stop(id) {

                clearInterval(id);

            }

        })

    </script>
</head>

<body>
<div id="a" class= g >
    <div class="output"></div>
    <input id="Button1" type="button" value="stop" class="bt" />
</div>

<div id="b">
    <div class="output"></div>
    <input id="Button2" type="button" value="stop"  class="bt"/>
</div>

<div id="c">
     <div class="output"></div>
    <input id="Button3" type="button" value="stop" class="bt" />
</div>


</body>
</html>
问题回答

间隔使用全局变量。

var interv = null;

interv = setInterval(function { ... }, 5000);

$( #btn ).on( click , function(){
    if (interv) clearInterval(intev);
})

您与 set Interval 关联的引用可能不在您的中键处理器的范围之内 。

$("#start").on("click", function(){
  var interv = setInterval(function(){ /*...*/ }, 1000);
});

$("#stop").on("click", function(){
  clearInterval(interv);
});

在以上代码中, 我们的 < code> interv 变量不在 # stop 按钮处理器的范围之内。 我们可以通过将它上移到另一个级别来改变它 :

var interv;

$("#start").on("click", function(){
  interv = setInterval(function(){ /*...*/ }, 1000);
});

$("#stop").on("click", function(){
  clearInterval(interv);
});

现在,两个处理器都可以访问 interv 变量。

看上去是范围界定问题与可互换使用 id DOM属性及 set Interval 响应值的组合。

<script type ="text/javascript">
    $(document).ready(function () {
        var timeouts = {};

        $( .output ).click(function () {
            var id = $(this).closest( .g ).attr( id );
            go(id);
        });

        $( .bt ).click(function () {
            var id = $(this).closest( .g ).attr( id );
            stop(id);
        });

        function go(id) {
            timeouts[id] = setInterval(function () {
                Chat_msg( Ch04 , id, u)
            }, 3000);
        }

        function stop(id) {
            clearInterval(timeouts[id]);
        }
    });
</script>

过去我讨论这个问题的方式是 使用一个函数 使用设定的超时来调用自己。

var stop = false
function caller  () {
  if (stop === true){
    //do something
    setTimeout(caller() , 1000);
  }
  else{
    //do something after button has  been click and stop is set to true
  }
}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签