我试图在bt_stop点击后停止循环。但它没有正常工作。单击停止按钮后,clearInterval不会被唤醒。
<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>
答案 0 :(得分:1)
这段代码中有相当多的缺失会让它起作用。至少有3个可预见的错误,有些是语法,有些是逻辑,所以我们走了!
1)$('bt_stop')和$('bt_go')应为$('#bt_stop')和$('#bt_go')
2)chatMSG甚至不存在于您的代码中,因此除非您将其包含在其他地方,否则将会出错。 (也可能输出outpu)
3)当调用Get_close()时,你不需要像这样单引号''#output'。你可以做'#output'
如果你做了所有这些事情,它应该有用。
答案 1 :(得分:0)