Solved

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

Posted on 2006-07-18
3
206 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

839 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