[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 997
  • Last Modified:

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
0
huji
Asked:
huji
  • 6
  • 6
2 Solutions
 
ZvonkoSystems architectCommented:
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>

0
 
ZvonkoSystems architectCommented:
Oh, and the setInterval() without a way to Clear the interval is an endless interval.
0
 
hujiAuthor Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ZvonkoSystems architectCommented:
Here a test for you:

<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;
}
</script>
<body>
<div onMouseOver="theDiv=this;show('theDiv')">Salaaaaaaaaaam</div>
</body>
</html>

0
 
hujiAuthor Commented:
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
0
 
ZvonkoSystems architectCommented:
And what is your question???
0
 
hujiAuthor Commented:
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
0
 
hujiAuthor Commented:
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.
0
 
ZvonkoSystems architectCommented:
As stated before, there is no way to pass an object parameter to setInterval() or setTimeout() call. All you can pass is one String that is interpreted as a statement.
Your string is this:
scr=setInterval("scroll(theDiv,100)",30)

I mean the passed String is: "scroll(theDiv,100)"
That String is evaluated and from the String you get a function call with an object reference as parameter and a number as parameter.
The object reference is evaluated out of the show() function scope. Therefore needs the name theDiv to be known when it is interpreted, it needs to be Global defined.
You have NO global assignment for theDiv var, but you use the fact that in IE all elements are registered as top elements and therefore are globaly accessible.
But that woul not work in Netscape.
In Netscape you have to distinguish betwean the "theDiv" element id and theDiv global var assignment.
Here a version which would work also in Netscape:
<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){
     var myDiv = document.getElementById(n);
     myDiv.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)
               myDiv.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>

Note the single quote characters in first setInterval() call.

This last version has significat difference to my initial version, so please do not confuse that two version.
And please ask for more details where I can continue to explain what there happens.

0
 
hujiAuthor Commented:
Excellent!
It worths an A+500.
Huji
0
 
hujiAuthor Commented:
Shall you have both "Good answer!" and "Good asist!" mails now!!?
;o)
0
 
ZvonkoSystems architectCommented:
Thanks :-)
FYI: I got one notification with "Good Assist!" and 2000 Expert Points. I was assisting myself ;-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now