admin管理员组文章数量:1386685
I have created functions that call other functions like
function abc()
{
def();
}
function def()
{
xyz();
}
Lets say def()
is called and at any moment I have a button
<button>STOP</button>
if I press it the execution terminates that means if the execution is run again it should run again from start.
I have seen other solutions in SO but they show how to pause a loop. Can anyone help me out?
I have created functions that call other functions like
function abc()
{
def();
}
function def()
{
xyz();
}
Lets say def()
is called and at any moment I have a button
<button>STOP</button>
if I press it the execution terminates that means if the execution is run again it should run again from start.
I have seen other solutions in SO but they show how to pause a loop. Can anyone help me out?
Share Improve this question asked Apr 26, 2017 at 1:57 AlenAlen 1,3016 gold badges24 silver badges45 bronze badges 1- You can't stop an already-running function by clicking a button. The button click event won't be processed until the current JS execution finishes. – nnnnnn Commented Apr 26, 2017 at 3:00
1 Answer
Reset to default 4You could use a variable to determine whether your code runs or terminates:
var abort = false;
function abc()
{
if (abort) {
return;
}
def();
}
function def()
{
if (abort) {
return;
}
xyz();
}
<button onclick="abort = true">STOP</button>
The only thing you'd have to add is to reset abort back to false whenever you're triggering the main functionality.
本文标签: How to stop function of javascript by pressing buttonStack Overflow
版权声明:本文标题:How to stop function of javascript by pressing button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744567146a2613118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论