Delay on 6 seconds before start scroller again ( in div )

Hi friends, please, how to adapt a delay on 6 seconds before start the div scroller again ?
I need, end the scroll, go back to the start, and pause for 6 seconds before starting again ( automatic ) the scroller.

Thanks !

This script:

Auto-scrolling a DIV- 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Auto Scroller</title>
<style type='text/css'>
#MyDivName {
  overflow:auto;
  width:200px;
  height:100px;
}
</style>
<script language="javascript">
window.onload = function() {
  scrollDiv_init();
};
ScrollRate = 10;
function scrollDiv_init() {
  DivElmnt = document.getElementById('MyDivName');
  DivElmnt.onmouseover = pauseDiv;
  DivElmnt.onmouseout = resumeDiv;
  ReachedMaxScroll = false;
  DivElmnt.scrollTop = 0;
  PreviousScrollTop  = 0;
  ScrollInterval = setInterval('scrollDiv()', ScrollRate);
}
function scrollDiv() {
  if (!ReachedMaxScroll) {
    DivElmnt.scrollTop = PreviousScrollTop;
    PreviousScrollTop++;
    ReachedMaxScroll = DivElmnt.scrollTop >= (DivElmnt.scrollHeight - DivElmnt.offsetHeight);
  }
  else {
    DivElmnt.scrollTop = PreviousScrollTop = 0;
    ReachedMaxScroll = false;
/*
    ReachedMaxScroll = (DivElmnt.scrollTop == 0)?false:true;
    DivElmnt.scrollTop = PreviousScrollTop;
    PreviousScrollTop--;
*/
  }
}
function pauseDiv() {
  clearInterval(ScrollInterval);
}
function resumeDiv() {
  PreviousScrollTop = DivElmnt.scrollTop;
  ScrollInterval = setInterval('scrollDiv()', ScrollRate);
}
</script>
</head>
<body>
<div id="MyDivName">
11111 11111 11111<br>22222 22222 22222<br>text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text text text<br>Almost Last Row<br>text text text<br>Last Row
</div>
</body>
</html>

Modify the “else” statement in your scrollDiv() script like this:

else { 
    clearInterval(ScrollInterval);
    DivElmnt.scrollTop = PreviousScrollTop = 0;
    ReachedMaxScroll = false;
    setTimeout("resumeDiv()",6000);
  }

Now works very well. Thank you very much ! :smile:

Please, It is possible to call a php variable in the code, I need to control the scroll speed and the delay by php, I made this change but it did not work:

<script language="javascript">
window.onload = function() {
  scrollDiv_init();
};
var ScrollRate = '<? echo $rate; ?>';
function scrollDiv_init() {
  DivElmnt = document.getElementById('scroll');
  DivElmnt.onmouseover = pauseDiv;
  DivElmnt.onmouseout = resumeDiv;
  ReachedMaxScroll = false;
  DivElmnt.scrollTop = 0;
  PreviousScrollTop  = 0;
  ScrollInterval = setInterval('scrollDiv()', ScrollRate);
}
function scrollDiv() {
  if (!ReachedMaxScroll) {
    DivElmnt.scrollTop = PreviousScrollTop;
    PreviousScrollTop++;
    ReachedMaxScroll = DivElmnt.scrollTop >= (DivElmnt.scrollHeight - DivElmnt.offsetHeight);
  }
  

else { 
    clearInterval(ScrollInterval);
    DivElmnt.scrollTop = PreviousScrollTop = 0;
    ReachedMaxScroll = false;
    setTimeout("resumeDiv()",1000);
  }

/*
    ReachedMaxScroll = (DivElmnt.scrollTop == 0)?false:true;
    DivElmnt.scrollTop = PreviousScrollTop;
    PreviousScrollTop--;
*/

}
function pauseDiv() {
  clearInterval(ScrollInterval);
}
function resumeDiv() {
  PreviousScrollTop = DivElmnt.scrollTop;
  ScrollInterval = setInterval('scrollDiv()', ScrollRate);
}
</script>

Try removing the single quotes.

I did this, with single quotes, and not single quotes, but it does not work :worried:

For the PHP to work, the file needs to be “,php” and not “.html” (well, unless you have things configured for it) I’m assuming you know this but just in case.

Anyway, my guess is that “short tags” aren’t enabled. Try

var ScrollRate = '<?php echo $rate; ?>';

Yes, PHP page, I know. But it does not work.
I have other variables that I capture via GET in other scripts. All other working. But in this case it is as if there is nothing in the ScrollRate item, it only recognizes a direct number. Weird.

If you look at the section of the page that has the JavaScript code in view-source it looks like an empty String?

var ScrollRate = '';

What does the PHP code that assigns the value to $rate look like?

Just as a side note, you should never evaluate code like this without need – it’s both dangerous and slow. Instead, you can simply pass the function itself to the timeout like

setTimeout(resumeDiv, 1000);

You might have a look at the MDN page on eval() for details why such string representations should be avoided.

1 Like

I work with Joomla CMS, in Joomla’s structural logic I use an xml page for data entry, and GET on the PHP page to capture the data:

XML page:

<field name="rate" type="text" default="70" label="" description=""/>

PHP page:

$rate = $params->get('rate');
var ScrollRate = '<?php echo $rate; ?>';

It works at all, only in this case no, if is blank it is not passing the variable.

This works perfectly for me:

setTimeout("resumeDiv()",1000);

``
But this one does not work:

setTimeout(resumeDiv, 1000);


The question is not this, but this:

var ScrollRate = ‘<? echo $rate; ?>’;


It does not work, does not get the variable. Any suggestion ?

Thanks

Finally I found the solution, now it works 100% for me, in this syntax :sweat_smile:

var ScrollRate = “<?php echo $rate = $params->get('rate'); ?>”;

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.