0

function callme(a) {
  setInterval(function() {
    console.log("fetch a" + a);
  }, 2000);
}
<button onclick="callme(1)">call 1</button>
<button onclick="callme(0)">call 0</button>

when 'call 1' button click output is

fetch 1
fetch 1
fetch 1
.
.
fetch 1

but when 'call 0' button click output becomes

fetch 1
fetch 0
fetch 1
fetch 0
fetch 1
.
.

how to stop previous loop execution ??


i want output like

when 'call 1' clicked

fetch 1
fetch 1
fetch 1
.
.

when 'call 0' clicked

fetch 0
fetch 0
fetch 0
.
.
mplungjan
  • 169,008
  • 28
  • 173
  • 236

0 Answers0