•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 375,199 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,021 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 756 | Replies: 5
![]() |
what i want to do is this:
but setInterval will not carry over the properties of "this".
when this happens:
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!
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);
}
}when this happens:
var apple=new foo(5,27); 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!
Last edited by hunkychop : Apr 16th, 2008 at 5:29 pm.
toast
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.
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.
Last edited by MidiMagic : Apr 16th, 2008 at 8:03 pm.
Daylight-saving time uses more gasoline
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); Last edited by hunkychop : Apr 16th, 2008 at 8:21 pm.
toast
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
you can do like this:
JavaScript Syntax (Toggle Plain Text)
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);} }
Last edited by peter_budo : Apr 19th, 2008 at 8:35 am. Reason: Keep It Organized - please use [code] tags
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> "I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: GetElementsByTagName Problem
- Next Thread: Drop shadow around content?



Linear Mode