Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2005
    Location
    Massachusetts, US
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts

    setInterval() and variables

    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:
    Code:
    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
    Last edited by Forever Zero; 08-15-2008 at 01:35 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    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);
      });
    };
    Trinithis

  • Users who have thanked Trinithis for this post:

    Forever Zero (08-15-2008)

  • #3
    New Coder
    Join Date
    Apr 2005
    Location
    Massachusetts, US
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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:
    Code:
    return f.apply(context, args);
    Do you know why this might be?
    Last edited by Forever Zero; 08-14-2008 at 08:04 PM.

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Replace
    Code:
    scrollInterval = setInterval(Function.bundle(this, "scroll", -1), 100);
    with:
    Code:
    scrollInterval = setInterval(Function.bundle(this, this.scroll, -1), 100);
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    @dumpfi: Shouldn't matter because of
    Code:
    if(typeof f == "string" && context)
      f = context[f];
    Forever Zero, have you defined scroll yet?
    Trinithis

  • #6
    New Coder
    Join Date
    Apr 2005
    Location
    Massachusetts, US
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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:
    Code:
        Event.observe(topMiddle, "mouseover", function(e){
            // scroll up (-1)
            scrollInterval = setInterval(Function.bundle(this, "scroll", -1), 100);
        });
    Last edited by Forever Zero; 08-14-2008 at 11:06 PM.

  • #7
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Oh, I misread Event.observe (which I'm guessing is like addEventListener).

    Code:
    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);
      });
    };
    Trinithis

  • #8
    New Coder
    Join Date
    Apr 2005
    Location
    Massachusetts, US
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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!
    Last edited by Forever Zero; 08-15-2008 at 01:43 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •