javascript - Trying to understand how to use clearInterval with JS -
this question has answer here:
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
Post a Comment