experimenting with the module pattern and instances
javascript is interesting
by jack gold
HTML
<a id="makeSession">Click me</a>
<a id="startTimer">Start Timer</a>
JavaScript
(function () {
function SessionContainer(id) {
this.sid = id;
this.timeLapse = 2000;
}
SessionContainer.prototype.getId = function () {
return this.sid;
};
SessionContainer.prototype.initTimeout = function () {
var now = new Date(),
sid = this.getId();
clearTimeout(this.timer);
this.timer = setTimeout(this.onSessionEnd, this.timeLapse);
console.log('Timer for SessionContainer #' + sid + ' started at ' + now + ' and should expire in ' + this.timeLapse + ' ms');
};
SessionContainer.prototype.onSessionEnd = function () {
var sid = this.getId();
console.log('All done with SessionContainer #' + sid);
};
SessionContainer.prototype.init = function () {
var sc = this;
var sess = sc.getId;
console.log(this);
document.querySelector('#startTimer').onclick = function () {
sc.initTimeout(sess);
}
};
window.SessionContainer = function (id) {
return new SessionContainer(id);
};
document.querySelector('#makeSession').onclick = function () {
var sc = new SessionContainer(5);
sc.init();
}
})();