Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fire event on radio group after some time delay

I am searching for a solution to let an event fire on a radio group after some time delay.

Specifically, I need to ask users a question and want to submit their input without having the users click a separate button. At the same time, it shall be possible that the user clicks different radio buttons while considering the answer.

onInput and onChange are both fired immediately after a user click. However, is there another solution?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is small example:

<div class="form-check form-check-inline"> 
  <label> What is 17 * 21? </label>
  <br></br>      
  
  <input class="form-check-input" type="radio" id="MC_0" name="MC_first" value="1" oninput="console.log('here');">
  <label for="MC_0" class="form-check-label">263</label>

  <input class="form-check-input" type="radio" id="MC_1" name="MC_first" value="2" oninput="console.log('here');">
  <label for="MC_1" class="form-check-label">357</label>
</div>

I would love to have oninput only fire after, say, 5 seconds without activity in this radio group.

>Solution :

You can use setTimeout

var timer = undefined;
function setTimer(time) {
  if (timer) {
    clearTimeout(timer);
  }
  timer = setTimeout(() => { console.log('abc'); }, time);
}
<button onclick="setTimer(2000)">Click Me</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading