Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the HTML DOM event doesn't affect immediately on the same DOM element?

I'm writing the code to change the Button element value when the click event is executing.

But Until it finishes it doesn't affect the button element value.

I attached the fiddle example which will give you a good idea of what I'm talking about.

function myFunction() {
 document.getElementById("demo").innerHTML = "Hello World";
 alert('hi')
}

Even the button element innerHTML changed before alert, it doesn't affect the UI.

How can I accomplish, before the alert is executed I need to change the button text?

Thanks.

like image 398
Irf92 Avatar asked Dec 17 '25 15:12

Irf92


1 Answers

Closest to threading in JS is settimeout function, this is not ok to do but here is how would it look like if you use it

 function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World";
  setTimeout(function(){
      alert('hi')
  },1000)
}
like image 136
Armin Avatar answered Dec 20 '25 05:12

Armin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!