Solved

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

Posted on 2006-07-18
3
201 Views
Last Modified: 2006-11-18
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
Comment
Question by:whaleyk
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 17129395
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
 

Author Comment

by:whaleyk
ID: 17129977
you are the best!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17130004
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now