...

View Full Version : Resolved setInterval() and variables



Forever Zero
08-14-2008, 04:54 PM
Hello!

I'm having trouble making a call to setInterval() using a local variable in the arguments.

Here is a very stripped down version of the method:


scroller.prototype.draw = function(){
var that = Object.clone(this);

var topMiddle = new Element("div", {
'class': 'scrollerTopMiddle'
});

Event.observe(topMiddle, "mouseover", function(e){
// scroll up
var scrollInterval = setInterval("that.scroll(-1)",100);
});

Event.observe(topMiddle, "mouseout", function(e){
// stop scrolling
clearInterval(scrollInterval);
});

}


The error I get is:
"that is not defined"

Any help would be greatly appreciated

Trinithis
08-14-2008, 05:56 PM
Function.bundle = function(context, f, args) {
if(typeof f == "string" && context)
f = context[f];
if(arguments.length < 3)
args = [];
else if(!(args instanceof Array))
args = Array.prototype.slice.call(arguments, 2);
return function() {
return f.apply(context, args);
};
};

scroller.prototype.draw = function() {
var scrollInterval;
var topMiddle = new Element("div", {
'class': 'scrollerTopMiddle'
});
Event.observe(topMiddle, "mouseover", function(e) {
// scroll up
scrollInterval = setInterval(Function.bundle(this, "scroll", -1), 100);
});
Event.observe(topMiddle, "mouseout", function(e) {
// stop scrolling
clearInterval(scrollInterval);
});
};

Forever Zero
08-14-2008, 07:00 PM
Thanks a lot for the response.

I am having a problem with this though. When I apply this code, whenever I hover over the scrollerTopMIddle part, the error is now:
"f is undefined"

The error occurs on this line:

return f.apply(context, args);

Do you know why this might be?

dumpfi
08-14-2008, 08:31 PM
Replace
scrollInterval = setInterval(Function.bundle(this, "scroll", -1), 100);with:
scrollInterval = setInterval(Function.bundle(this, this.scroll, -1), 100);dumpfi

Trinithis
08-14-2008, 09:02 PM
@dumpfi: Shouldn't matter because of

if(typeof f == "string" && context)
f = context[f];

Forever Zero, have you defined scroll yet?

Forever Zero
08-14-2008, 09:57 PM
I have defined scroll (I put it as the first function just to make sure). Also, changing it to this.scroll did not seem to help any.

If it helps at all, here is the method for scroll:


scroller.prototype.scroll = function(dir){
var that = Object.clone(this);

if (dir == 1) {
currentTop -= 10;
$("scrollerContent_" + that.id).style.top = currentTop + "px";
}
else
if (dir == -1) {
// Start scrolling up
currentTop += 10;
$("scrollerContent_" + that.id).style.top = currentTop + "px";
}
};


And here is what I put in my JS file:


Event.observe(topMiddle, "mouseover", function(e){
// scroll up (-1)
scrollInterval = setInterval(Function.bundle(this, "scroll", -1), 100);
});

Trinithis
08-15-2008, 12:27 AM
Oh, I misread Event.observe (which I'm guessing is like addEventListener).


scroller.prototype.draw = function() {
var self = this;
var scrollInterval;
var topMiddle = new Element("div", {
'class': 'scrollerTopMiddle'
});
Event.observe(topMiddle, "mouseover", function(e) {
// scroll up
scrollInterval = setInterval(Function.bundle(self, "scroll", -1), 100);
});
Event.observe(topMiddle, "mouseout", function(e) {
// stop scrolling
clearInterval(scrollInterval);
});
};

Forever Zero
08-15-2008, 12:34 AM
That worked perfectly. Sorry about that, I should've mentioned I was using the Prototype framework.

(Once again) Thank you very much for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum