Pause running using Sweetalert (swal, Javascript)

Asked

Viewed 351 times

-1

Hello, Basically I have a while in Javascript running, depending on the result it presents me an informative message, I want to display this informative message and continue the execution after the user click on "OK".

Currently I am using Javascript Alert, as it only proceeds the code when the user clicks the button.

alert("Usuário sem info cadastrada");

I wish to use the Sweetalert library, because it creates friendlier alerts, I am creating an alert as follows.

swal("Ops!", "Usuário sem info cadastrada", "error");

The problem is that while keeps running, without waiting for the user to click the OK button, one alert replaces the other. I need while to continue only if the user clicks the button, so that the user can view all the problems that are occurring.

1 answer

1

To pause a while, you can use . Like the framework Sweetalert2 works with this, so just use the operator await.

Example:

document.querySelector('button')
  /**
   * Utiliza o operador `async` para informar que é
   * uma função assíncrona (Obrigatório)
   */
  .addEventListener('click', async ev => {
    
    /* Inicia o contador */
    let i = 0
    console.group('While')

    while (i < 10) {
      console.log( i )

      if (i % 2) {
        /**
         * Utiliza o operador `await` para informar
         * ao JavaScript que é para esperar o resultado
         * da função antes de prosseguir
         */
        await Swal.fire('Any fool can use a computer')
      }
      
      i++
    }
    
    console.groupEnd()
  })
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js"></script>

<!--
Polyfill para IE11
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill"></script>
-->

<button type="button">Start</button>

Complements:
/a/280445/99718
/a/273010/99718

Browser other questions tagged

You are not signed in. Login or sign up in order to post.