Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 29th, 2004, 08:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default JS/ASP watch

Hi.
I'm trying to make a JS watch where I get the initial values from the server. Can't make it work though and can't find the problem.:(
 Any help is appreciated!

Here's my code:
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<head>
<title>Development of an ASP/JS watch</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
  <!-- 
    function Watch(){    //initialize the Watch object and get the time variables from the server using ASP
      this.prototype.second = <%= Second(Now) %>;
      this.prototype.minute = <%= Minute(Now) %>;
      this.prototype.hour = <%=  Hour(Now) %>;
    }
    function goWatch(){    //make the watch tick
      Watch.second++;
      if (Watch.second == 60){
        Watch.second = 0;
        Watch.minute++;
      }
      if (Watch.minute == 60){
        Watch.minute = 0;
        Watch.hour++;
      }
      if (Watch.hour == 24){
        Watch.hour = 0;
      }
      return Watch.hour, Watch.minute, Watch.second;
    }
/*    function getSeverTime(){    //get the time variables from the server using ASP
      Watch.prototype.iniSecond = <%= Second(Now) %>;
      Watch.prototype.iniMinute = <%= Minute(Now) %>;
      Watch.prototype.iniHour = <%=  Hour(Now) %>;
    }
*/
    function normalize(hour, minute, second){    //Convert 0 seconds to 00 seconds, etc.
      if (second < 10){
        second = '0' + second;
      }
      if (second < 10){
        minute = '0' + minute;
      }
      if (hour < 10){
        hour = '0' + hour;
      }
      return hour + ':' + minute + ':' + second;
    }
    function startWatch(){
      Watch()
      setInterval("normalize(goWatch())", 1000);
      document.showWatch = Watch.hour + Watch.minute + Watch.second;
    }
  -->
</script>
</head>

<body onload="startWatch();">
Klokken er <div id="showTime"></div>
</body>
</html>
Regards Jon :)
__________________
- mega
Aspiring JavaScript Ninja
 
Old April 29th, 2004, 09:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

My mozilla JavaScript Console says that startWatch isn't defined. What does that mean?

 - mega
 
Old April 29th, 2004, 09:24 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey Mega,

This message means that when you body onload call to startWatch() is executed, the function startWatch cannot be found, although it's present in your code, any syntax errors in it or above it in your code block can cause this message to appear.

HTH,

Chris

 
Old April 29th, 2004, 10:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Aha I thought. I'll just put that function at the top of my script, that way I know if it is that function or some other that produces errors.
But now my js console doesn't say anything. Do you see any mediate flaws in my code?
I even put try catch around my code..
Code:
function startWatch(){
  try {
    Watch();
    setInterval("normalize(goWatch())", 1000);
    document.showWatch = Watch.hour + Watch.minute + Watch.second;
  } catch(e){
    alert("Failure in the Watch object!\nDescription: "+e.description);
    return;
  }
}
 - mega
 
Old April 29th, 2004, 11:26 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Mega,

quite a few problems with the code, have modified it slightly, give this a go...

<html>
<head>
<title>Development of an ASP/JS watch</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--//
// make the watch a global object
var gWatch;

function Watch(pName, pTargetTagId, pHour, pMinute, pSecond){
    // set properties for this instance of the watch, not prototypes
    this.Hour = pHour;
    this.Second = pSecond;
    this.Minute = pMinute;
    this.Name = pName; // store global name of watch so we can set intervals for function calls
    this.Target = document.getElementById(pTargetTagId); // locate div to display time in
    this.Timer = null; // store handle to interval here so we can stop if we want
}

Watch.prototype.Start = function(){
    if(this.Timer == null){
        this.Timer = setInterval(this.Name + ".Tick();", 1000);
    }
};

Watch.prototype.Stop = function(){
    clearInterval(this.Timer);
    this.Timer = null;
};

Watch.prototype.Tick = function(){
    with(this){
        Second++;
        if(Second == 60){
            Second = 0;
            Minute++;
        }
        if(Minute == 60){
            Minute = 0;
            Hour++;
        }
        if(Hour == 24){
            Hour = 0;
        }
    }
    this.Target.innerHTML = this.GetTimeString();
};

Watch.prototype.GetTimeString = function(){
    // when building string version, do not assign values to objects hour, minute, second properties
    // - otherwise they get concatenated & will become 000000n etc
    var temp = "";
    with(this){
        if(Hour < 10){
            temp += '0';
        }
        temp += Hour + ":";

        if(Minute < 10){
            temp += '0';
        }
        temp += Minute + ":";

        if(Second < 10){
            temp += '0';
        }
        temp += Second;
    }
    return temp;
};

function startWatch(){
    // create a new instance of the watch object, assign hour minute second here as well
    // that way we can have more than one watch with different times
    gWatch = new Watch("gWatch", "showTime", <%= Hour(Now) %>, <%= Minute(Now) %>, <%= Second(Now) %>);
    gWatch.Start();
}
//-->
</script>
</head>

<body onload="startWatch();">
Klokken er <div id="showTime"></div>
</body>
</html>

HTH,

Chris


 
Old April 29th, 2004, 03:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Thanks!
As you can see I'm fairly new to js. I heard a while back that innerHTML only worked in IE, but found out that mozilla (and therefore NN6) supports it. But still using DOM methods should be more efficient, just don't know what that exactly means (I'll look into that).
Thanks again Chris you really did a lot more that you had to.

 - mega





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop watch rohit_ghosh Access VBA 2 May 3rd, 2007 04:14 AM
asp inside js crmpicco Classic ASP Basics 2 July 6th, 2005 09:49 PM
Stop Watch Paulsh Access VBA 3 September 22nd, 2004 12:10 PM
Watch Window is disabled LaFeverMF ADO.NET 1 July 21st, 2004 11:32 PM
digital watch StreetSweaper C# 3 December 30th, 2003 05:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.