www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Trying to call a function when system clock reach a certain time.

  1. #1
    Join Date
    Oct 2012
    Posts
    14

    Question Trying to call a function when system clock reach a certain time.

    Please assist me. I the code is set to alert when the clock reaches a certain time then alert which works.

    The problem I am having how is each tick of time after the time the alert continues to popup.

    Each day at the chosen time it should pop up just like an alarm clock.


    Code:
    // JavaScript Document
    
    $(window).load(function (){
    $("#submit").click(function () {	
    var myVar=setInterval(function(){myTimer()},1000);
     	
    });
    var c = 0.01;
    var clock = "15:48:00";
    
    function myTimer()
    {
    var d=new Date();
    var t=d.toLocaleTimeString();
    document.getElementById("time").innerHTML=t;
    
    if(document.getElementById("time").innerHTML >= clock)
    {
    	alert("I got one more piece almost completed");
    clock="00:00:00";
    }
    
    }
    
    
    function callPow(){
       var val= document.getElementById("txt").value;
       var power= document.getElementById("txt2").value;
    	alert(Math.pow(val,power));
     }
     
     
     
    });

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255
    Look at the value of your 't' variable and compare it to the value of 'clock'.
    Do they look similar?

    Different problem: Where do you set the value of 'clock' to ever equal the current time? Only happens once a day in military time.

    Finally, what does your 'callPow()' function have to do with the clock display?

  3. #3
    Join Date
    Oct 2012
    Posts
    14
    callPow is the function that is going to be called every 24 hours just once.

    I need there current time to check against the clock is set here var clock = "15:48:01";

    When I click the submit button the check for the current time is recording and the clock starts when it reaches "00:00:01" run the callPow function.
    save the value of the callPow function in a xml document.

    day to do the same.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255
    Quote Originally Posted by postonoh View Post
    callPow is the function that is going to be called every 24 hours just once.

    I need there current time to check against the clock is set here var clock = "15:48:01";

    When I click the submit button the check for the current time is recording and the clock starts when it reaches "00:00:01" run the callPow function.
    save the value of the callPow function in a xml document.

    day to do the same.
    Two problems to address since you have not provided the information in your post.
    1. Where are you calling the "callPow()" function to see the action?
    2. Your check of >= clock (assuming they are of similar format) will be true from "15:48:01" until "23:59:59".
    Is that what you want to do in your design?

  5. #5
    Join Date
    Oct 2012
    Posts
    14
    // JavaScript Document

    $(window).load(function (){
    $("#submit").click(function () {
    var myVar=setInterval(function(){myTimer()},1000);

    });
    var c = 0.01;
    var clock = "15:48:00";

    function myTimer()
    {
    var d=new Date();
    var t=d.toLocaleTimeString();
    document.getElementById("time").innerHTML=t;

    if(document.getElementById("time").innerHTML >= clock)
    {
    callPow();
    clock = false;
    }

    }

    function callPow(){

    If(document.getElementById("txt2").value == "");
    {
    var val = c;
    }
    else
    {
    var val = document.getElementById("txt2").value;
    }

    var power= document.getElementById("txt2").value;
    alert(Math.pow(val,power));
    }

    });

    Ok. What I am trying to do is once a day the clock turns to "15:48:00" when it reaches this time call callPow();

    Then reset then day two at 15:48:00 call callPow, day three same time call callPow, and so on.

    I

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255

    Exclamation

    You have a problem with this statement:
    Code:
    function callPow(){
    
    If(document.getElementById("txt2").value == "");  // If cannot be capitalized.  Cannot end with a ';' here.
    {
    var val = c;
    }
    else
    {
    var val = document.getElementById("txt2").value;
    }
    I would dispute your statement in post #1 where you say the program works!
    Do you have a live site to look at and test. Not your whole site, just this section with the problem in action.

    Also, you should enclose your script between [ code] and [ /code] tags (without the spaces)
    to make it easier to analyze your scripts.

  7. #7
    Join Date
    Oct 2012
    Posts
    14
    The callPow is not my problem. I will fix this later I need to make sure the the clock version work first. Focusing on the callPow is secondary.

    If I get the clock to work.I can add just the alert to see if it called everyday then I will fix the callPow

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255

    Lightbulb

    Quote Originally Posted by postonoh View Post
    The callPow is not my problem. I will fix this later I need to make sure the the clock version work first. Focusing on the callPow is secondary.

    If I get the clock to work.I can add just the alert to see if it called everyday then I will fix the callPow
    If it is not a part of the problem, then leave it out. It still won't work as is.

    Your problems:
    1. Your contents of the <div> will not equal your setting as programmed.
    2. Your comparison will always find any value > "1" to be after "15"
    3. Any value less then 10 will display as only 1 digit. Not good for your '00' comparison logic
    4. Your .toLocalTimeString() function adds an 'AM' or 'PM' which will not compare to your 'clock' setting.
    5. If you don't solve one problem at a time, you are in for a lot of trial & error debugging!

    Try this...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title> Untitled </title>
    </head>
    <body>
    <div id="timeDIV"></div>
    
    <script type="text/javascript">
    Number.prototype.padDigit = function() { return (this < 10) ? '0'+this : this; }
    
    var clock = "15:48:00";
    function myTimer() {
      var d=new Date();
      var t = d.getHours().padDigit()+':'+d.getMinutes().padDigit()+':'+d.getSeconds().padDigit();
      if(t >= clock) { t += ' is AFTER '+clock; }
                else { t += ' is BEFORE '+clock; }
      document.getElementById("timeDIV").innerHTML = t;
    }
    window.onload = function() { var t = setInterval("myTimer()",1000); }
    </script>
    
    </body>
    </html>

  9. #9
    Join Date
    Oct 2012
    Posts
    14
    With this code will run once a day will test. the callPow is function the allows me to multiply the var c that = 0.01 to the 2 power example.

    if day one equal 0.01 day two would equal 0.01 + 0.01 = 0.02 day three would equal 0.02 + 0.02 = 0.04. I am testing a theory use the 2 power in math. how could I do this base on a 24:00 time clock.

  10. #10
    Join Date
    Oct 2012
    Posts
    14
    With this code will run once a day will test. the callPow is function the allows me to multiply the var c that = 0.01 to the 2 power example.

    if day one equal 0.01 day two would equal 0.01 + 0.01 = 0.02 day three would equal 0.02 + 0.02 = 0.04. I am testing a theory use the 2 power in math. how could I do this base on a 24:00 time clock.

  11. #11
    Join Date
    Oct 2012
    Posts
    14
    thanks by the way.

  12. #12
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255
    Incrementing a value of 0.1 by a power of 2 will take over 561 years to reach 20:48 (which would be just a bit less than 24:00).

    I'm not sure I would be willing to wait for that long. Are you using this clock for carbon dating???

  13. #13
    Join Date
    Oct 2012
    Posts
    14
    No actual I am trying to test a theory I was told that if you save a penny and double it everyday in 30 days you will have over a million dollars saved. I just want so if I did this on a web page and just let it sit for 30 and see the return.

  14. #14
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255

    Lightbulb

    You can make the solution much simpler if you would state the problem to start with...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title> Untitled </title>
    </head>
    <body>
    <input type="text" id="startAmount" value="0.01">Start amount<br>
    <input type="text" id="daysToDouble" value="30">Days to double<br>
    <button onclick="calcValue()">Calculate</button><br>
    <input type="text" id="endingSumAmount" value="" readonly>Sum Results<br>
    <input type="text" id="endingMultAmount" value="" readonly>Mult Results<br>
    
    <script type="text/javascript">
    function calcValue() {
      var startAmount = parseFloat(document.getElementById('startAmount').value);
      var sumAdd = 0;
      var sumMult = startAmount;
      for (var i=0; i<parseInt(document.getElementById('daysToDouble').value); i++) {
        sumAdd += startAmount;
        sumMult *= 2;
      }
      document.getElementById('endingSumAmount').value = sumAdd.toFixed(2);
      document.getElementById('endingMultAmount').value = sumMult.toFixed(2);
    }
    </script>
    
    </body>
    </html>
    Actually, it is closer to $10 million.
    Start saving your pennies!

  15. #15
    Join Date
    Oct 2012
    Posts
    14

    Talking

    I will next time thanks. I just trying to figure out the code myself and get help along the way. What I was really looking for was a way to let sit on my computer for 30 days and see the it calculate each day. But with this I think I can figure it out. I am trying to teach kids the possibility of saving. The code sit on a pc in the classroom and each day for 30 days the will the value change to the new value.


    Thanks for your help and assistance.
    Last edited by postonoh; 02-13-2013 at 08:37 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles