Chinovski Chinovski - 23 days ago 5
Javascript Question

How to pause an event in javascript?

I'm trying since yesterday with my js code but it still doesnt work. So I have a select list, when I change the selected option it calls an onchange event that calls a DWR function.

The problem is the DWR function takes a while and resets my select options (the first element selected instead of the one selected), I tried to set the previous value but it works only when I add a while loop.

var valeurTypeUo = document.getElementById('selectElement').value;
// DWR function called by this function
forme.getOptions(params);
// console.log(document.getElementById('typesUoChoix').value) is empty String
while (document.getElementById('typesUoChoix').value != valeurTypeUo)
document.getElementById('typesUoChoix').value = valeurTypeUo;


This is my code, it works but there is always an alert if I want to stop the script. Is there any way to replace this while instruction.

Thank you in advance.

Answer

You can use setInterval which you can clear instead of while.

Your code can be like:

   var stopCheking = false;
   var checkingInterval; // interval holder

   var valeurTypeUo = document.getElementById('selectElement').value;

   // DWR function called by this function
   forme.getOptions(params);

   // console.log(document.getElementById('typesUoChoix').value) is empty String

   checkingInterval= setInterval( function() { 
     if( stopCheking ) clearInterval(checkingInterval);
   }, 2);

When you want to stop the event (interval) in this case, you set the stopCheking flag to true.