Solved

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

Posted on 2006-07-18
3
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

751 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