Is it possible to somehow invoke a non-global function (i.e. a method of a class) from a timer?
The following example shows what I'd like to achieve:
function MyClass() {
var msg = 'no message';
// shows an alert after some delay
this.delayedAlert = function(message) {
msg = message;
// global function works:
//window.setTimeout('globalShow("'+message+'")', 1000);
// this obviously also works:
//this.show();
// this doesn't work: *************
// I'd like to invoke the show() method of this class after some delay
window.setTimeout('this.show()', 2000);
};
this.show = function() {
alert(msg);
};
}
$(document).ready(function() {
$('p').click(function() {
var c = new MyClass();
c.delayedAlert('hello');
});
});
function globalShow(msg) {
$('#hello').html(msg);
}
You can find a running sample here: http://jsbin.com/aqako5