Solved

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

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

770 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