You may be looking at this problem the wrong way. It's probably not the best idea to halt your program since you want execution to happen as quickly as possible. One of the big advantages to JavaScript is that it has an awesome event API baked in to it.
Instead of making your program wait/sleep, try using events:
drawrectangle(a, b, c, d);
Let's make a function that will run when we detect click events:
function customClickEventHandler(event) {
drawrectangle(e, f, g, h);
}
And finally, bind your new function to the window's 'click' event. You can choose any DOM element, not just the window.
window.addEventListener('click', customOnClickEvent);
Documentation
Events API: https://developer.mozilla.org/en/docs/Web/API/Event
Follow this link to find out more about the 'addEventListener' function:
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
Update
If you want to add multiple handlers, it's easy:
function eventHandler1(event) {
// do something...
}
function eventHandler2(event) {
// do something else...
}
window.addEventHandler('click', eventHandler1);
window.addEventHandler('click', eventHandler2);