| hunkychop | Apr 16th, 2008 5:28 pm | |
| setInterval inside function issue what i want to do is this:
function foo(param1, param 2){this.bar=function(){this.intervalID=setInterval(this.fbar,5000); }
this.fbar=function(){//(do stuff with the properties of "this"
alert(this.intervalID); }
}
but setInterval will not carry over the properties of "this".
when this happens:
var apple=new foo(5,27);
apple.bar();
i need apple.fbar(); to repeat automatically by force of apple.bar();
note: fbar does not have to be inside of the foo function but i do need to get all of "this"'s properties to it durring a setInterval
thanks for the help! |
| MidiMagic | Apr 16th, 2008 8:02 pm | |
| Re: setInterval inside function issue You have a scoping problem.
Your values created in foo are local to that function. They don't exist outside the bounds of that function.
Declare some global variables at the top of your .js file. Then assign the values created inside foo to those variables. |
| hunkychop | Apr 16th, 2008 8:18 pm | |
| Re: setInterval inside function issue thanks, i did alittle bit of googling on js globals and found i could attach the variable on to the window object. so.. my script ends up looking like this.... maybe?
function foo(param1,param2){
window.currentObject=this;
window.currentObject.fruit=param1;
window.currentObject.newfruit=param2;
window.currentObject.bar=function(){
window.currentObject.intervalID=setInterval("window.currentObject.fbar();",5000);
}
window.currentObject.fbar=function(){
var blah=window.currentObject.fruit;
window.currentObject.fruit=window.currentObject.newfruit;
window.currentObject.newfruit=blah;
}
window.currentObject.end=function(){
clearInterval(window.currentObject.intervalID);
}
}
var apple=new foo(apple,orange);
apple.bar();
setTimeout("apple.end();",60000);
|
| hunkychop | Apr 16th, 2008 8:52 pm | |
| Re: setInterval inside function issue i guess here is my new question. how can i transfer all of the properties of window.currentObj to 'this'? |
| wuzhiye | Apr 17th, 2008 11:59 am | |
| Re: setInterval inside function issue you can do like this:
function foo(param1, param 2){
var me = this;
this.bar=function(){
this.intervalID=setInterval(function(){me.fbar.call(me);},5000);
}
this.fbar=function(){//(do stuff with the properties of "this"
alert(this.intervalID);}
} |
| ~s.o.s~ | Apr 17th, 2008 2:07 pm | |
| Re: setInterval inside function issue To get around these scope issues, you can either go with the messier and error prone global object solution or make use of the elegant concept of Closures.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv"Script-Content-Type" content="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Expires" content="0"> <!-- disable caching -->
<title>Example</title>
<script type="text/javascript">
function Klass(name) {
this.name = name;
this.handle = null;
this.startTimer = function() {
this.handle = setInterval(function(obj) {
return(function() {
alert(obj.name);
});
}(this), 5000);
}
var a = new Klass('hello');
a.startTimer();
</script>
</head>
<body>
</body>
</html> |
| All times are GMT -4. The time now is 7:23 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC