0

I have an HTML page featuring a div with right-to-left scrolling text; the following JavaScript is located between the HEAD tags of the document.

function scroll(oid, iid) {
            this.oCont = document.getElementById(oid);
            this.ele = document.getElementById(iid);
            this.width = this.ele.clientWidth;
            this.n = this.oCont.clientWidth;
            this.move = function() {
                this.ele.style.left=this.n + "px"
                this.n--
                if(this.n<(-this.width)){this.n=this.oCont.clientWidth}
            }
        }
        var vScroll
        function setup() {
            vScroll = new scroll("oScroll", "scroll");
            setInterval("vScroll.move()", 20);
            }           
        onload = function(){
            setup()
        }

        $("scroll").hover(function() {
            $("scroll").stop(true, false)
        }, function(){
            scroll();
        });
        scroll();

The scrolling text works fine; however I wanted the scrolling to stop on mouse hover. Although the text does stop scrolling when the mouse cursor passes over the div, I get a javascript error "Object expected". I'm new to javascript and have no idea where I'm going wrong.

Any help would be greatly appreciated.

3
  • 2
    which line does the error point to? It looks like you're trying to reference this outside of the proper context Commented Sep 25, 2013 at 14:40
  • The page is actually being displayed in a WebBrowser control in a simple WPF application; I think this must be an IE browser; changing the default browser does not affect the functioning of the error message.. Commented Sep 25, 2013 at 14:45
  • @user1748443: Object expected is an IE error. All other browsers give a much more descriptive and useful error message. For example: Chrome says vScroll is not defined. Commented Sep 25, 2013 at 14:58

1 Answer 1

1

Your problem is with your setInterval. You are passing it a string! This makes it use eval! That means the code is ran in global scope, so vScroll does not exist.

Instead, pass a function to setInterval:

setInterval(function(){
    vScroll.move();
}, 20);

The function passed to setInterval is called with the "context" (this value) set to null, so you cannot pass vScroll.move directly to setTimeout. You can, however. do:

setInterval(vScroll.move.bind(vScroll), 20);

but this doesn't work in all browsers.

P.S. Passing a string to setInterval is bad practice, you should always be passing a function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.