Link to home
Start Free TrialLog in
Avatar of huji
hujiFlag for United States of America

asked on

scroll down effect, problem with setInterval

Hi
I wanted to put a DIV in a page, then every time the user moves mouse over it, I wanted it to clip downward. (To have something like an scroll efect).
I tried this first:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script>
function show(x){
      setInterval("scroll("+x+")",100)
}
function scroll(n){
      window.status+=n
}
</script>
<body>
<div onMouseOver="show(this)">Salaaaaaaaaaam</div>
</body>
</html>

In this model, show function would be used for setting the visibility of DIV. (I need this, because I want to set the visibility of some other DIVs at the same time).
The variable sent to it is the DIV object, obviously. Now the scroll function (which must be later coded to clip the DIV object), contains only a test code. If it is run correctly, it must fill the status bar with [object] s.
It failed! It said, "object is undifined"
I changed the show(x) function to such:

function show(x){
      setInterval("scroll(x)",100)
}

It gave me the same error message.
I thought this is because I am trying to send the X variable to a function outside the show(x) function, and thus, X is undifined to the scroll(n) function. So:

function show(x){
      window.status+=x.toString()
      setInterval("show("+x+")",100)
}

It again failed! This time one [object] was shown in the status bar (because windo.status command was run once, when it was called by the mousover event.) But it failed to repeat.

I have always used setInterval and setTimeout for functions that do not need arguments,ie setInterval("func()",time), and not for those which need arguments, ie setInterval("func(arg)",time). But I need this now.

Any ideas?

A simple code that can result in a drop down effect is excellent!

Huji
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

As you have noticed yourself, you do NOT pass the object, you pass a String to the setInterval() call. The String is like this: "[object]"

To avoid that string problem you have to create a global var and assign to it the reference.
Like this:
<div onMouseOver="theDiv=this;show('theDiv')">Salaaaaaaaaaam</div>

Oh, and the setInterval() without a way to Clear the interval is an endless interval.
Avatar of huji

ASKER

Hi
1)Now all that is sent to my show() function is a string 'theDiv' and I can not use it to locate the DIV to which I want to aplly changes. This does not meet my needs.
2)Obviously I would change the simple code posted above, and remove the bugs like the one you noticed about setInterval()...
Thanks, and waiting for an apropriate solution.
Huji
SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of huji

ASKER

I made a little change to the code you posted:


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script>
function show(x){
     setTimeout("scroll("+x+")",100)
}
function scroll(n){
     window.status+=n.innerHTML;  //**************I made a change here, to track the repeat.
}
</script>
<body>
<div onMouseOver="theDiv=this;show('theDiv')">Salaaaaaaaaaam</div>
</body>
</html>


Here is the result of the test:
The scroll function is called only ONE time, we have only ONE "Salaaaaaaaaaam" in the status bar.. But for scrolling reasons, I need the scroll function to be called EVERY 100 milliseconds, to let the menu glide down little by little.
Your code does not meet my needs, unfortunately.
Waiting for help

Huji
And what is your question???
Avatar of huji

ASKER

I again ask what I need:
I need a piece of code/idea which.... when a user clicks on a part of a page (a DIV in specific words), another piece of the page is scrolled, moved, gileded, anything you call it.
The important thing is, the part of the page that is gonna be affected does not have a FIXED hard coded name, it name may change from page to page, and thus I need to use variables for refering to it. I prefer this to be in DHTML, since the DOM idea is very easy, but does not cover all browser types of interest.
Thanks
Huji
Avatar of huji

ASKER

Hi
I implemented the code which resulted to this. It is working! But I have a theorical question. Look:



<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script>
var i=10;
function show(x){
      document.getElementById(x).style.visibility="visible"
      document.getElementById(x).style.clip="rect(10px 100 0 0)"
      height=document.getElementById(x).offsetHeight
      window.status=height
      scr=setInterval("scroll("+x+","+height+")",30)
}
function scroll(n,h){
      n.style.clip="rect(0px 100px "+i+"px 0px)"
      if((i/h)<0.9){i=i+10}
      else{i=i+1}
      if(i>=100){
            clearInterval(scr)
                  n.style.clip="rect(0px 100px "+h+"px 0px)"
      }
}
</script>
<body>
<div onMouseOver="show('theDiv')" style="background-color:#EEEEEE;width:100;height:100">Salaaaaaaaaaam</div>
<div id="theDiv" style="background-color:#EEEEFF;top:20;left:110;width:100;height:100;z-index:1;position:absolute;visibility:hidden;">Salaaaaaaaaaam</div>
</body>
</html>


Here, what is sent to show(x) function as it's x variable is a string ('theDiv'). Until the last line of show(x) function, if I ask for the variable type of x, it says that x is a string.
Now just the same x is sent to the scroll function in the last line of show(x) function. But from the first lin of scroll function if I ask for the variable type of n (which must be the same as x) it is an object. The nodeName returns "DIV"!!!
It means that n is not equal to x string but it is equal to document.getElementById(x) !! As far as I know, I didn't send the object to the scroll(n) function!
Please help me understand why it happens!

Thanks a lot
Huji

PS:It seems that setInterval function has some unknown sides for me. I will increase the points to 500, If you help me understand it well.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of huji

ASKER

Excellent!
It worths an A+500.
Huji
Avatar of huji

ASKER

Shall you have both "Good answer!" and "Good asist!" mails now!!?
;o)
Thanks :-)
FYI: I got one notification with "Good Assist!" and 2000 Expert Points. I was assisting myself ;-)