Button Click

JavaScript permits async functions as their event handler. We do so to play the tune, but we have to also intermittently disable the button. To have an async function in JavaScript one can use the async keyword. The main method is now declared as follows; we also use the conven-tion to change its name to append _async to the function name:

    async function main_async() {
document.getElementById("launch").disabled = true;
document.getElementById("demo").innerHTML = "";
clear();
let text=document.getElementById("text").value;
await consult_async(text);
document.getElementById("launch").disabled = false;
}

Inside the async main function there are some novel statements. We do access the launch button and set it to disabled at the beginning of the async main function. This is to avoid that the launch button is fired multiple times. We then await the Dogelog player concult_async() call and only when we pass this point, we enable the launch button again.

    document.getElementById("launch")
.addEventListener("click", main_async);

Interestingly programmatically the registration of the click event listener doesn’t change much. Except that we now register the new async main function, there is nothing in the syntax that would hint a registration of an async event listener.

Kommentare