javascript - Stop a loop with an click event jQuery -
I'm looking for a way to stop a loop with a click event (type panic button). If you click on the button you should close it immediately.
// Basic Ideas
$ Each (some or more, function (index, value) {setTimeout (function () {Console.log (index);}, 5000 * index);}); $ ('# Panic-Button'). Click (function () {// code should stop looping}}:
After the loop starts, you can enter $ with the click handler. Each can not interrupt the loop. Javascript execution is single-threaded, so the next time the thread will be queued, the click handler will be queued - after the loop is over. However, as the timer is executed asynchronously, you can cancel them by placing and pasting each of your IDs:
var Timeouts = []; $ .eee (some array, function (index, value) {timeouts.push (set timeout (function () {console.log (index);}, 5000 * index));); $ ('# Panic-Button'). Click on (function () {$ .ee (timeout, function (_, id) {explicit timeout (id);}); timeout = [];}): < / Html>
Comments
Post a Comment