I have a loop using setTimeout like bellow:
<button onclick="clickStop()">stop</button>
<script type="text/javascript">
let i = 0;
let sett;
function timeOut(counter) {
sett = setTimeout(function () {
$.get("./test3.php", {data_id: counter}, (data) => {
console.log(data);
i++;
timeOut(i);
});
}, 1000);
if(counter >= 10) {
clearTimeout(sett);
alert("Done!");
}
}
timeOut(i);
function clickStop() {
clearTimeout(sett);
}
</script>
But when i click button stop it's not working, help me!!
$.getreturns, the callback will just start it again when it$.getcall the callback. Maybe you could haveclickStopset a flag that is checked before$.getcalls the function.