Why clearInterval not working... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why clearInterval not working...

var stop = setInterval(autoFill,200); function autoFill(){ $(".ui-autocomplete-input:eq(0)").val("NEW DELHI - NDLS"); $(".ui-autocomplete-input:eq(1)").val("SHMATA VD KATRA - SVDK"); $(".hasDatepicker:eq(0)").val("19-05-2018"); $("[name='jpform:jpsubmit']:eq(0)").click(function(){clearInterval(stop);}); $("[name='jpform:jpsubmit']:eq(0)").click();}

18th May 2018, 5:27 PM
Sanjeev Kumar
Sanjeev Kumar - avatar
5 Answers
+ 4
Fata1 Err0r well once before I made a script for a website...and I put the click event inside the function...and it worked well....but this time not working...I don't know....but the problem may be somewhere else....
18th May 2018, 8:11 PM
Sanjeev Kumar
Sanjeev Kumar - avatar
+ 4
Fata1 Err0r I used setTimeout to execute the function and it is also putting the function in loop... amazing
18th May 2018, 8:38 PM
Sanjeev Kumar
Sanjeev Kumar - avatar
+ 3
Button is getting clicked and the function bound to it is also being executed....but clearInterval didn't remove the timer....and function keeps on looping....
18th May 2018, 5:29 PM
Sanjeev Kumar
Sanjeev Kumar - avatar
+ 3
Try putting your click event outside of the function that's being looped with the interval. I put together a quick example for you so you can see it in action. If you post a link to your whole code, I can correct it if you can't get it working. EXAMPLE: https://code.sololearn.com/WUqY88ljyPtV/#html <!DOCTYPE html> <html> <head> <title>Page Title</title> <script src="https://code.jquery.com/jquery-3.1.1.js"></script> </head> <body> <button id="clearInt" >Clear</button> </body> <script> var stop = setInterval(function(){ console.log("Running"); }, 1000); $('#clearInt').click(function(){ clearInterval(stop); console.log("Stopped"); }); </script> </html>
18th May 2018, 6:15 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Fata1 Err0r I got you....
18th May 2018, 7:34 PM
Sanjeev Kumar
Sanjeev Kumar - avatar