FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Javascript timer - need help :(

 


sonam
I need some help with Javascript. I got some result from one function (e.g. name "A"). Now I need function what will check every few seconds is this result different or same. If is different then I need to recalculate function "A" if is not just start new timer.

Sonam
Peterssidan
You can use the setTimeout function to get a function called in the future.
It can work something like this:
Code:
// call checkA(); after 1 second
setTimeout("checkA();", 1000);

var oldA;

function checkA() {
  if(A() != oldA) {
    //  recalculate function "A"
  } else {
    // start new timer
    setTimeout("checkA();", 1000);
  }
}
grinta
setTimeout would pretty much be the way to go.
I would even leave out the else case

Code:
var oldA = "";
setTimeout("checkA();", 1000);

function checkA() {
  if(A() != oldA) {
    oldA = A();
  }
    setTimeout("checkA();", 1000);
 }
Star Wars Fanatic
You don't need use settimeout, if you use setInterval it will automatically call it every second.

Code:
var oldA = "";
setInterval(checkA, 1000);

function checkA() {
  if(A() != oldA) {
    oldA = A();
  }
 }
sonam
I don't know but I cannot understand Javascript logic. With PHP I am fine but JS is not going in my mind. Here is two function what I have. First one check height of document:

Code:
<script language="JavaScript" type="text/javascript">
function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
var e = (Math.round( getDocHeight()*0.64);
</script>


Second one difine scroller width/height:
Code:
<script type="text/javascript">
var scroller  = null;
window.onload = function () {
  scroller  = new jsScroller(document.getElementById("News"), 400, 300);
  scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, true);
}
</script>


OK, problem is next. I need implement var e from first function in second function in scroller instead of 300 because I would like to dinamically change height of scrollbar. On the end I need to check this every few seccond and do nothing if is height same or resize if height is changed.
Star Wars Fanatic
Well you could do it this way:
Code:
<script language="JavaScript" type="text/javascript">
function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
</script>

Excluding the var e. Then this function:
Code:
<script type="text/javascript">
var scroller  = null;
function () setScroller {
  scroller  = new jsScroller(document.getElementById("News"), 400, (Math.round( getDocHeight()*0.64) );
  scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, true);
}
</script>


That we it calls the function each time it is run, and no need for the var e.
Finally:
Code:
<script type="text/javascript">
window.onload = setScroller ;
setInterval(setScroller, 1000);
</script>


You might want to put it all in one js file and just include the file, but that looks like it should work.


Note: This code hasn't been tested at all, just what I was thinking at the time. This is how I would go about it.
sonam
Something is going wrong. Shocked Maybe is better to go step by step. First I need to check every few seconds this function:
Code:
<script language="JavaScript" type="text/javascript">
function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
</script>


I was try to insert something like:
Code:
window.onload = getDocHeight;
setInterval(getDocHeight, 1000);


but this is not working. I don't know why?

Sonam
Star Wars Fanatic
Well that will work, but you need to do something with the number that getDocHeight returns. Just calling the function does nothing. You need to call another function which compares what getDocHeight returns, and what it was previously.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.