PDA

View Full Version : TimeOut scope problems..


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

liorean
02-22-2008, 02:48 AM
Try the latter code snippet with the following replacement:this.timer = function(){
setTimeout(this.clock, 100)
}
this.timer();changed to setTimeout(
function(obj,method){
var
args=Array.prototype.slice.call(arguments,2);
return function(){
return obj['method'].apply(obj,args);
};
}(this,'clock'),
100);or if you want to split it out:function mkMethodCaller(obj,method){
var
args=Array.prototype.slice.call(arguments,2);
return function(){
return obj['method'].apply(obj,args);
};
}

/* ... */

setTimeout(mkMethodCaller(this,'clock'), 100);

winthers
02-22-2008, 10:44 AM
Hey thx :)
I cant say i fully understand what is going on in that code..well i have a clue, but im sure after doing a little search on the web, it will make perfect sens..

Never seen that used before..
args=Array.prototype.slice.call(arguments,2);


..But i looked it up at the web, and for thoes, who like me dont know whats going on in that pice of code, I found this awsome tut..link below..

Posted both a a direct link (iligal i know ;P) but made up 4 it posting the page link.. dittn i ?! ;P

link to the site: http://shifteleven.com/
link to the tutorial: http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript

liorean
02-22-2008, 11:56 AM
Hey thx :)
I cant say i fully understand what is going on in that code..well i have a clue, but im sure after doing a little search on the web, it will make perfect sens..

Never seen that used before..
args=Array.prototype.slice.call(arguments,2);
It's quite simple: arguments is array-like but does not inherit from Array.

What I want to do is to create a new array args which contains all the elements in arguments except the first two. If arguments had inherited from Array, I could have done that like this:var
args=arguments.slice(2);

It's not that easy, however. Since arguments does not inherit from Array I need to somehow *pretend* that it is an array in order to slice the two first elements out of it. Thankfully, most of the Array.prototype methods are written such that you can use them on any object and not just arrays. Which means all I have to do is call the Array.prototype.slice method with the this-object set to arguments. Like this:var
args=Array.prototype.slice.call(arguments,2);

.But i looked it up at the web, and for thoes, who like me dont know whats going on in that pice of code, I found this awsome tut..link below..

Posted both a a direct link (iligal i know ;P) but made up 4 it posting the page link.. dittn i ?! ;PSpamming and explicit advertising (i.e. off-topic linkage elsewhere) is against the forums rules, but posting a link to an article that explains something that is entirely on topic is quite okay. So relax, you didn't do anything that we consider bad.

winthers
02-22-2008, 12:40 PM
Nice. thx for helping me out, and thx for showing me new ways to use javascript :D