Stop setInterval call in JavaScript

前端 未结 16 1845
星月不相逢
星月不相逢 2020-11-21 05:00

I am using setInterval(fname, 10000); to call a function every 10 seconds in JavaScript. Is it possible to stop calling it on some event?

I want the us

16条回答
  •  心在旅途
    2020-11-21 05:56

    Try

    let refresch = ()=>  document.body.style= 'background: #'
      +Math.random().toString(16).slice(-6);
    
    let intId = setInterval(refresch, 1000);
    
    let stop = ()=> clearInterval(intId);
    body {transition: 1s}

提交回复
热议问题