0

UPDATE: Added a try catch block around the problematic code and it doesn't enter the catch block nor the finally block. It just exits. So, i've been trying to get this page to work. After reading about it on this question and trying to implement several similar approaches, somehow my page crashes. After using chrome's F12 to go step by step, I've found out that its when I call the sleepNow that it goes to create the new Promise that makes JS crash and the execution stops.

async function validateDates(){
<code>
        var settings = 'dialogwidth:292px;dialogheight:140px;status:no;help:no;resizable:no;menubar:no;toolbar:no;';
        var popup = function_that_opens_window("random.jsp?scoIdlist="<VARS SET IN CODE>",window,settings); 
try {       
  const sleepNow = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
        while(popup && !popup.closed){
             await sleepNow(1000);
        }
}
            catch(err) {
                alert("ERROR: "+ err.message);
            }
            finally{
                alert("no error: ");
                }
}

Anyone has any idea how that's happening or why? Thanks in advance

Thuga PT
  • 23
  • 5
  • 1
    This looks fine. What exactly do you mean by "*crash*"? – Bergi Sep 03 '20 at 19:28
  • The current async function stops at that point, and the rest of the scripts that are running also stops. – Thuga PT Sep 04 '20 at 09:17
  • 1
    Sounds like the window is reloading. What exactly does `function_that_opens_window` do? – Bergi Sep 04 '20 at 09:23
  • It checks for what browser is opening the window and set the style, then it does window.open(url, '_blank', ), then it also sets some actions for the window, such as onclick, onfocus, window.setInterval This window that's opened closes after doing some validations, with a window.close on the function that's passed to it's onload – Thuga PT Sep 04 '20 at 09:38
  • Added a try around the entire thing, and after it goes through the promise it doesn't stop at the catch nor the finally. Updated the question with the new code – Thuga PT Sep 04 '20 at 11:13
  • What I think might be happening is that this validateDates() function is the onSubmit of the form, and the same form also has a submit button that onClick sets the action of said form as the next page.(document.frm1.action = '../finish.jsp). What I'm thinking is happening is that somehow the flow is cut when the promise is made and it jumps from the validateDates() function to the page on the frm1.action – Thuga PT Sep 04 '20 at 13:39
  • Oh, when you are submitting a form it will navigate to the given target page. It does indeed unload your page, stopping all scripts. You will need to call `.preventDefault()` on the submit event to prevent that from happening if you don't want the form to actually submit until the popup is closed. – Bergi Sep 04 '20 at 14:14
  • Where would you do that? Can you do it on the tag? Or in a piece of javascript? I tried to add document.getElementById("cmdSubmit").addEventListener("click", function(event){ event.preventDefault() }); before the popup call and am still getting redirected – Thuga PT Sep 07 '20 at 17:06
  • The listener should be on the `submit` event on the `
    ` element. And you wouldn't install it right before opening the popup - it might already be too late by then - but on page load (or form creation, if dynamic).
    – Bergi Sep 07 '20 at 17:19
  • Is there a way to allow the submit after that? I want to stop the unload until I'm done with validations, but after that, I want the form to submit. Is there any way to do that? – Thuga PT Sep 07 '20 at 17:26
  • Yes, calling [`formElement.submit()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit) can be used to submit the form, and doesn't trigger the event (and doesn't get prevented). – Bergi Sep 07 '20 at 18:04
  • @Bergi It did work, thanks! – Thuga PT Sep 11 '20 at 10:17

0 Answers0