javascript - Trying to understand how to use clearInterval with JS -


i using javascript , trying write function make colors blink problem when click on toggle blink button not stop blinking when click stop toggle end button. trying implement clearinterval make stop no luck. appreciated.

function main() {   $('.select-color').on('click', function() {     var selectedcolor = $(this).attr('class');      switch (selectedcolor) {   case 'select-color cyan not-selected':     colorclass = 'cyan';     break;   case 'select-color yellow not-selected':     colorclass = 'yellow';     break;   case 'select-color magenta not-selected':     colorclass = 'magenta';     break;     }     $(this).removeclass('not-selected');     $(this).siblings().addclass('not-selected');   });    var colorclass = '';     $('.box').on('click', function() {         $(this).toggleclass(colorclass);     });    $('.toggle-blink').on('click', function() {     if (colorclass) {         $('.toggle-blink').toggleclass('opacity');     }     setinterval(function() {           $('.box.cyan, .box.yellow, .box.magenta').toggleclass('blink');                       }, 500);      });    $('.toggle-blink-end').on('click', function() {    scope.clearinterval(main);   });  }  $(document).ready(main); 

clearinterval() takes intervalid argument, not function reference, need store when calling setinterval() returns given intervalid:

var intervalid ;  intervalid = setinterval( ... );  clearinterval(intervalid); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -