winthers
02-21-2008, 10:18 PM
Hello first of all sry for my pure englich..
Im having a hard time getting this small "class" act like i want it to,
my goal is to make this timeOut and the function/functions that it will call, live in the same class, knowing as little about the world around it as posible.
in the first block of code below, im getting close but i still need to feed it a refrence to that instence, that is calling the class. not really what i want sins i cant really generate new instence in a loop ? (let me know if im way off)
function myClass(_elemenID){
this.elemenID = _elemenID
this.count = 0;
//methods
this.clock = myClock;
this.timer = myTimer;
}
function myClock(){
document.getElementById(this.elemenID).innerHTML = "time : " + this.count++
this.timer()
}
function myTimer(){
var t = setTimeout( function(){ objClass.myClock() }, 100)
}
var objClass = new myClass('test');
window.onload = objClass.timer;
In this next bit of code im getting a tad closer, making the timerOut function, and the function called by the timeOut, live in the same function/class,
but the problem is that i cant really pass any arguments to that class with out breaking it.. like the elementID.... i blame my pure understanding of prototyping... but hope ther is a way to do that.
function test(){
}
function myClock(){
alert("lol")
var count = 0;
this.clock = function(){
document.getElementById('test').innerHTML = "time : " + count++
this.timer()
}
this.timer = function(){
setTimeout(this.clock, 100)
}
this.timer();
}
test.prototype.startTimer = myClock;
var objClock = new test();
window.onload = objClock.startTimer;
It would be cool if i cut make it work so it dit not need to know who called it.
so i cut make some made loop spamming 100 instence at one time..
Maby the anwser to my problem is pretty simple, but im stil a newbee, so please let me know if there is a way to fix my "class"
//Martin
Im having a hard time getting this small "class" act like i want it to,
my goal is to make this timeOut and the function/functions that it will call, live in the same class, knowing as little about the world around it as posible.
in the first block of code below, im getting close but i still need to feed it a refrence to that instence, that is calling the class. not really what i want sins i cant really generate new instence in a loop ? (let me know if im way off)
function myClass(_elemenID){
this.elemenID = _elemenID
this.count = 0;
//methods
this.clock = myClock;
this.timer = myTimer;
}
function myClock(){
document.getElementById(this.elemenID).innerHTML = "time : " + this.count++
this.timer()
}
function myTimer(){
var t = setTimeout( function(){ objClass.myClock() }, 100)
}
var objClass = new myClass('test');
window.onload = objClass.timer;
In this next bit of code im getting a tad closer, making the timerOut function, and the function called by the timeOut, live in the same function/class,
but the problem is that i cant really pass any arguments to that class with out breaking it.. like the elementID.... i blame my pure understanding of prototyping... but hope ther is a way to do that.
function test(){
}
function myClock(){
alert("lol")
var count = 0;
this.clock = function(){
document.getElementById('test').innerHTML = "time : " + count++
this.timer()
}
this.timer = function(){
setTimeout(this.clock, 100)
}
this.timer();
}
test.prototype.startTimer = myClock;
var objClock = new test();
window.onload = objClock.startTimer;
It would be cool if i cut make it work so it dit not need to know who called it.
so i cut make some made loop spamming 100 instence at one time..
Maby the anwser to my problem is pretty simple, but im stil a newbee, so please let me know if there is a way to fix my "class"
//Martin