Here's a complete html document that demonstrates my problem:
<html>
<head>
<title>Test</title>
</head>
<script type="text/javascript">
function messageDisplayer(message) {
this.alertMessage = message;
}
messageDisplayer.prototype.showMessage = function() {
alert( this.alertMessage );
};
function start() {
var displayer = new messageDisplayer("Show this message");
displayer.showMessage();
window.setTimeout( displayer.showMessage, 1000 );
}
</script>
<body onLoad="start()">Test</body></html>
When this page is loaded, I immediately get a 'Show this message' alert, but when the timeout kicks in a second later, it gives an alert with 'undefined'. When I set a breakpoint on that alert, this
is apparently the window itself, not my messageDisplayer
object the second time it hits it.
How can I get that function to refer to the alertMessage
property when it's called by timeout?