简体   繁体   中英

How enable & disable some javascript function using radio button

HTML

<div id="radio_btn">
  <ul class="tg-list">
    <li class="tg-list-item">
        <input class="tgl tgl-skewed" id="cb3" type="checkbox"/>
        <label class="tgl-btn" data-tg-off="OFF" data-tg-on="ON" for="cb3"></label>
    </li>
  </ul>
</div>

I want to disable & enable the javascript code (below) using the above radio button.

Javascript

window.setInterval(function() {
  var elem = document.getElementById('box2');
  elem.scrollTop = elem.scrollHeight;
}, 1000); 

There is no radio button. But to toggle the interval, save an ID you can clear

var tId;
$(function() {
  $("#cb3").on("click", function() {
    if (this.checked) tId = setInterval(
      function() {
        var elem = document.getElementById('box2');
        elem.scrollTop = elem.scrollHeight;
      }, 1000);
    else clearInterval(tId);
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM