toggle button text on a timer with javascript

I have a working toggle button text code but I would like the button to toggle back to the original state after 4 seconds. I have tried looking for a solution and can’t wrap my head around whether I need to use a set interval function or settimeout function and how to implement them exactly. Hopefully a javascript wizard can help me out here.

function myFunctionbutton() {
  var x = document.getElementById("singleadd");
  if (x.innerHTML === "add to bag") {
    x.innerHTML = "Loading...";
  } else {
    x.innerHTML = "add to bag";
  }
}
<button id="singleadd" onclick="myFunctionbutton()">add to bag</button>

86 thoughts on “toggle button text on a timer with javascript”

Leave a Comment