RG1nine
RG1nine

Reputation: 11

Await function on button click

async function init() {
  $("#present").slideDown(100);
  setTimeout(btnClose, 4000);
  function btnClose(){ 
    $("#present").slideUp(100);
  }
  $("#present").click(async function() {
    await timerOn();
  });
  moveFunction();
}

My init function is invoked on a button click... after that a new button appears on screen.... i want to call timerOn function but on a button click... now note one thing i have added 'async' in onClick because it gives an error that await can be used only in async function....

My purpose is moveFunction should be called only after timerOn is executed completely...

Will it serve the purpose?? plzzz help...

Upvotes: 0

Views: 2153

Answers (1)

Rajiv
Rajiv

Reputation: 36

Since the buttons will be generated dynamically then the scripts will be unaware of the recently created DOM elements, so you will have to delegate the event to the button like the following (assuming you are using a class .present on your buttons):

 
$( "body" ).delegate( "button.present", "click", function() {
  //do your stuff here
});

Upvotes: 1

Related Questions