In JavaScript functions are called using the follow syntax:
functionName()
When using an event handler, each event will call the functions assigned as event handlers. You do not want to call the function but instead set the function value as the event handler, like this:
function alertTime(inputTime) {
inputTime = Date.now();
alert(inputTime);
}
element.onclick = alertTime;
This is why the alert appears immediately: you are calling your function, instead of assigning it to the onclick event handler.
You will notice this doesn't pass along the testTime variable. While you could bind the current value of testTime, "", for every single time the event handler is called, like this:
element.onclick = alertTime.bind(this, testTime)
...it is most likely not what you want, as the event handler will always be called with "" even if testTime changes. Instead, try using testTime inside your function directly.