1

I'm trying since yesterday with my js code but it still doesn't 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?

8
  • 1
    Maybe this might help : stackoverflow.com/questions/951021/… Commented Nov 17, 2016 at 10:13
  • 1
    Can you show the forme.getOptions(params) function? Does DWR have a callback that you can propogate? Commented Nov 17, 2016 at 10:17
  • 1
    You can delay the execution of a block of code, but other code on the same level before that delay will still execute -- this is because as far as I'm aware, setTimeout is not asynchronous. Commented Nov 17, 2016 at 10:24
  • @reporter it shows me this error > SyntaxError: missing ; before statement I did the same like on the example Commented Nov 17, 2016 at 10:25
  • @DavinTryon There is not a call back, but instructions that take time to execute like Collections.sort (the code of DWR is a composed of more than a hunderd line. Commented Nov 17, 2016 at 10:31

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You can "pause" functions with ES6 generators(yield keyword), but normally you should not need it in this case. Plain callback on the async call should do it.

Anyways here is the link for generators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.