How to fire jQuery function on click event but only after the page is loaded?

Schws

I have a button that requests information from another site. This call is quite large and takes a couple of seconds to receive the response. Once I receive the response I want to display a modal to the user with some info from the response.

So my question is, how do I show the modal on click of my button, but only once I receive the payload back with the info needed to construct the message? Please take note that the web service call is not done in JavaScript. I am storing my message to the user in a hidden element on the page for the JS to pick up.

Here is what I have tried and hopefully explains what I am trying to accomplish.

$("#get_cost").click(function() {
    var myMessage = $("#responseEle").val();
    swal("Success!", myMessage, "success");
}).ready();

Btw... swal is just https://sweetalert.js.org/ which is really cool but not related to my question.

Adrian Brand

You can poll every second for the condition you are looking for with a timeout. Run checkDone function on the button click.

let done = false;

function checkDone() {
  if (done) {
    console.log('Done');
  } else {
    console.log('Polling');
    setTimeout(checkDone, 1000); // Run checkDone again in 1,000 milliseconds
  }
}

checkDone();

setTimeout(() => { done = true; }, 5000);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fire an event only after element is loaded?

How to fire an event function only after another event is fired?

How to call jquery function then fire click event of button?

jQuery .on click event doesn't always fire on Ajax loaded content

JQuery: How to fire click event on hidden element?

How to make sure that click event will fire only after http call come back using RxJS

How to fire the function useEffect after clicking event?

Checkbox change event only fire after reloading page

binding jquery function with form loaded after page

How to fire a jquery function onchange from an html element in highchart event on click?

How to enable .click event if it is called from a function it should not be loaded if the page is loading directly

How to fire jQuery function only if form is valid

How to have click event ONLY fire on parent DIV, not children?

fire server click event after executing a jquery ajax call from client click event

run jQuery click only after page loads

jquery, how to check if class is loaded. Then change function to hide after first click

jQuery: Dynamic click event only firing after second click

Click event to fire only on one element of a class

Jquery function only loads after page refresh

jQuery function running only after refreshing the page

Button click event only works when page is reloaded or alert in function

What causes jQuery click event not always to fire?

Fire vue method from jquery on click event

jQuery: fire click event on dynamically added HTML

jQuery: fire click() before blur() event

Fire ul tag click event not li with jquery

Click event doesn't fire in jquery

How to fire a function only after the first one has finished in (JavaScript)

how to make a function that executes an event click on jquery