1

I have created a timer to be used as a reference on a alert when the timer reaches exactly 5:00 pm/ 17:00. Do you have any ideas how could I do that? Do I check the time once in a while?

Here's how I create my timer.

 <script language="JavaScript">
    var tick;

    function stop() {
        clearTimeout(tick);
    }

    function usnotime() {
        var datetoday = document.getElementById("datenow")
        var currentDate = new Date()
        var day = currentDate.getDate()
        var month = currentDate.getMonth() + 1
        var year = currentDate.getFullYear()
        datetoday.val("" + month + "/" + day + "/" + year + "");

        var txt = document.getElementById("timeticker")

        var ut = new Date();
        var h, m, s;
        var time = "      ";
        h = ut.getHours();
        m = ut.getMinutes();
        s = ut.getSeconds();
        if (s <= 9) s = "0" + s;
        if (m <= 9) m = "0" + m;
        if (h <= 9) h = "0" + h;
        time += h + ":" + m + ":" + s;
        txt.innerHTML = time;
        tick = setTimeout("usnotime()", 1000);
    }
    //--> document.rclock.rtime.value=time;   tick=setTimeout("usnotime()",1000);    }  //-->
</script>

<tr>
    <td class="tdlabel">Current Time:</td>
    <td>
        <div id="timeticker" name="rtime" size="22"></div>
    </td>
</tr>

And it seems that my timer is not displaying. Any ideas??

muthu
  • 5,381
  • 2
  • 33
  • 41
Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94
  • what does "datenow" look like in var datetoday=document.getElementById("datenow") ; Can you give more HTML code to explain datenow element – WatsMyName Aug 10 '12 at 03:56
  • Check second answer to [this question](http://stackoverflow.com/questions/3830244/get-current-date-time-in-seconds), that's what you need I think. – elclanrs Aug 10 '12 at 04:00

4 Answers4

1

You have a mistake, you mixed javascript and jquery.

 datetoday.val("" + month + "/" + day + "/" + year + "");

Should be (assuming datetoday is a textfield. If div use innerHTML instead of value)

datetoday.value="" + month + "/" + day + "/" + year + "";

and add just before the closing script tag

window.onload=function(){
    usnotime();
}
WatsMyName
  • 4,240
  • 5
  • 42
  • 73
  • Nice! Got it to work sir. Thank you for the answer despite lacking of codes. You really got what I want to ask there. Thanks again! – Luke Villanueva Aug 10 '12 at 05:14
0

Not sure if this is the exact answer; it might help posting a bit more code.

  1. I don't see how you're starting the method. You might need to add a button-execute / page-load call of usnotime().
  2. Do you have real objects for all of those variables? (I.e. timeticker, datenow).
  3. Check your JavaScript error log in the browser. It's a bit hard without more code for me to see what's wrong, so either include more, or use the debugger.
Michael
  • 1,014
  • 1
  • 8
  • 23
0

Using the setTimeout() u can do , Refer this it may be useful for you

Community
  • 1
  • 1
muthu
  • 5,381
  • 2
  • 33
  • 41
  • Regarding the link you gave? I really can't quite understand the code. When should I trigger the alert? and it seems that the alert in the code has no trigger. Can you elaborate? Thanks. – Luke Villanueva Aug 10 '12 at 05:36
0

This may help you....

<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript"> 
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}

function display_ct() {
var strcount
var x 
var x1
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds= currentTime.getSeconds()
 x=month + "/" + day + "/" + year
if (minutes < 10){
minutes = "0" + minutes
}
x=x+"  "+hours + ":" + minutes +":"+seconds+ " "
x1=hours + ":" + minutes + ":"+seconds+" "
if(hours > 11){
x=x+"PM"
x1=x1+"PM"
if(x1=="5:30:0 PM"){alert(x1);}
} else {
x=x+"AM"
x1=x1+"AM"
if(x1=="8:16:0 AM"){alert(x1);}
}


document.getElementById('ct').innerHTML = x;
tt=display_c();
 }
</script>
</head>

<body onload=display_ct();>
<span id='ct' ></span>

</body>
</html>
  • Consider improving your answer by explaining what is going on in your code listing. That way, it will also be instructive to less experienced users. – Kris Jul 19 '14 at 07:44