1

I'm trying to stop a loop after bt_stop click. But it is not working fine. clearInterval is not woking after the stop button is clicked.

<div id="abc">
     <input id="bt_go" type="button" value="go" />
     <input id="bt_stop" type="button" value="stop" />
     <div id="output"></div>
</div>

<script>
     $('#bt_stop').click(function () {
        Get_close('','user1');//enter code here
     })

     $('#bt_go').click(function () {
        Get_close(''#output'',user1');
     });

     function Get_close(id, output) {
         if (id!= '') {
             id = setInterval(function () {
                  chatMSG(id, outpu);
             }, 1000)
         }
         else {
             clearInterval(id);
             alert('stop');
         }
     }
</script>
4

2 回答 2

1

这段代码缺少很多可以使它工作的东西。至少有 3 个可预见的错误,有些是语法,有些是逻辑,所以我们开始吧!

1) $('bt_stop') 和 $('bt_go') 应该是 $('#bt_stop') 和 $('#bt_go')

2)chatMSG 甚至不存在于您的代码中,因此除非您将其包含在其他地方,否则会出错。(也可能应该输出输出)

3) 调用 Get_close() 时,您不需要像这样使用双引号 ''#output''。你可以做'#output'


如果你做了所有这些事情,那么它应该工作。

于 2012-05-25T00:18:51.383 回答
0

为了使用它的 id 选择一个元素,你应该添加'#':

$('#bt_stop')

这是您尝试执行的操作的示例:http: //jsfiddle.net/FpgLh/

祝你好运!

于 2012-05-25T00:15:24.097 回答