Improve company productivity with a Business Account.Sign Up

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

Title: javascript... display delay, can anyone fix? (part 2... zvonko)

Taking several seconds for clock to start "ticking".  Can it be speeded up?
Thank you thank you zvonko!

================================================================


<html>
<head>
<title>Server time clock</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Tomleung (lok_2000_tom@hotmail.com) This tag should not be removed-->
<!--Server time ticking clock v2.0 Updated by js-x.com-->


var hours;
var minutes;
var seconds;
var timer=null;
function sClock()
{
<cfoutput>
  hours=#TimeFormat(Now(), "H")#;
  minutes=#TimeFormat(Now(), "mm")#;
  seconds=#TimeFormat(Now(), "ss")#;
</cfoutput>
  if(timer){clearInterval(timer);timer=null;}
  timer=setInterval("work();",1000);
}

function twoDigit(_v)
{
  if(_v<10)_v="0"+_v;
  return _v;
}

function work()
{
  if (!document.layers && !document.all && !document.getElementById) return;
  var runTime = new Date();
  //var dn = "";
  var shours = hours;
  var sminutes = minutes;
  var sseconds = seconds;
  if (shours ==0)
  {
    //dn = "PM";
    shours-=12;
  }
 
 
  if (!shours) shours = 12;
      sminutes=twoDigit(sminutes);
  sseconds=twoDigit(sseconds);
//  shours  =twoDigit(shours  );
  movingtime = ""+ shours + ":" + sminutes +":"+sseconds+"";
  if (document.getElementById)
    document.getElementById("clock").innerHTML=movingtime;
  else if (document.layers)
  {
    document.layers.clock.document.open();
    document.layers.clock.document.write(movingtime);
    document.layers.clock.document.close();
  }
  else if (document.all)
    clock.innerHTML = movingtime;

  if(++seconds>59)
  {
    seconds=0;
    if(++minutes>59)
    {
      minutes=0;
      if(++hours>23)
      {
        hours=0;
      }
    }
  }
}
</script>
</head>
<body onLoad="sClock();">
<span id=clock style="position:relative;"><cfoutput>#TimeFormat(Now(), "H:mm:ss")#</cfoutput></span><img src="/images/clear.gif" width="5">Eastern Time<br>
</body>
</html>
0
whaleyk
Asked:
whaleyk
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
I do not see why ColdFusion would delay the page for getting three timer vars, but at list the browser side script can be speed up by calling the write to screen imediately.
Like this:

<html>
<head>
<title>Server time clock</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Tomleung (lok_2000_tom@hotmail.com) This tag should not be removed-->
<!--Server time ticking clock v2.0 Updated by js-x.com-->

var cltTime = new Date();
<cfoutput>
  hours=#TimeFormat(Now(), "H")#;
  minutes=#TimeFormat(Now(), "mm")#;
  seconds=#TimeFormat(Now(), "ss")#;
</cfoutput>
var offSet=cltTime.getTime()-(new Date(cltTime.getFullYear(),cltTime.getMonth(),cltTime.getDate(),hours,minutes,seconds)).getTime();

function twoDigit(_v){
  if(_v<10)_v="0"+_v;
  return _v;
}

function work(){
  if(document.getElementById || document.all || document.layers){
    var runTime = new Date((new Date()).getTime()-offSet);
    var shours = runTime.getHours();
    var sminutes = runTime.getMinutes();
    var sseconds = runTime.getSeconds();;
    if (shours ==0){
      shours-=12;
    }
    if (!shours) shours = 12;
    shours  =twoDigit(shours  );
    sminutes=twoDigit(sminutes);
    sseconds=twoDigit(sseconds);
    movingtime = ""+ shours + ":" + sminutes +":"+sseconds+"";
    if (document.getElementById){
      document.getElementById("clock").innerHTML=movingtime;
    } else if (document.layers){
      document.layers.clock.document.open();
      document.layers.clock.document.write(movingtime);
      document.layers.clock.document.close();
    } else if (document.all) {
      clock.innerHTML = movingtime;
    }
    setTimeout(work,1000);
  }
}
</script>
</head>
<body>
<span id=clock style="position:relative;"></span><img src="/images/clear.gif" width="5">Eastern Time<br>
<script>work()</script>
</body>
</html>

The difference is that setInterval waits for the first time interval to expire.

0
 
whaleykAuthor Commented:
you are the best!
0
 
ZvonkoSystems architectCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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