Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Autoincrement Script

    My javascript skills are limited at best but I think it would be the best bet for what I need. I need a script that will autoincrement each day and if it gets to 365 I need it to continue to 366 etc. up to infinity. I also need something that will be easy to reset the script to 0.

    This is going to be for something similar to a Days Without an Accident which is going to run on a dedicated desktop and be displayed on a TV. There is no IIS, database, PHP, Mysql, etc. it is a simple company XP image.

    I just want to put the html file in the startup group and have it run each day when the computer is turned on. If the computer gets turned off and on mid day it should not update.

    This is all I could think of based on the limited materials I have to work with.

    If someone could help me out that would be great, or if anyone has a better idea based on the info provided, I am open.

    Much thanks

  • #2
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    javascript would really NOT be 'best' at all.

    JS cannot save files to the local machine so it would start at zero every time the page was loaded.


    WSH (vbs) is what you should use so it can write to a flat file database using the File System Object (FSO)
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by Beauford View Post
    My javascript skills are limited at best but I think it would be the best bet for what I need. I need a script that will autoincrement each day and if it gets to 365 I need it to continue to 366 etc. up to infinity. I also need something that will be easy to reset the script to 0.

    This is going to be for something similar to a Days Without an Accident which is going to run on a dedicated desktop and be displayed on a TV. There is no IIS, database, PHP, Mysql, etc. it is a simple company XP image.

    I just want to put the html file in the startup group and have it run each day when the computer is turned on. If the computer gets turned off and on mid day it should not update.

    This is all I could think of based on the limited materials I have to work with.

    If someone could help me out that would be great, or if anyone has a better idea based on the info provided, I am open.

    Much thanks

    Try this:-

    Code:
    <!DOCTYPE html>
    <head>
    </head>
    <body>
    
    <span id = "message"></span>
    
    <script type = "text/javascript">
    
    var startDate = new Date(2014,4,30);  // year,month,day - months in Javascript are 0-11
    var today = new Date();  // the date each day
    var oneday = 1000 * 60 * 60 * 24;  // The number of milliseconds in one day
    var dx_ms = startDate.getTime();  // Convert both dates to milliseconds
    var dy_ms = today.getTime();
    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(dy_ms - dx_ms);
    // Convert back to days
    var diff =  Math.floor(difference_ms/oneday);
    document.getElementById("message").innerHTML = "The number of days is without an accident is  " + diff;
    </script>
    
    </body>
    </html>

    The simplest way to rest the counter to 0 is to change the startDate value to the now current date. You could use a cookie, but the snag of that is if the cookie is deleted then the code reverts to the original start date.

    @chrishurst - why do you think that a file is required?


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 05-30-2014 at 06:44 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    For the same reason you use a cookie, and as you state a cookie can be lost or deleted which WILL start the count at zero again, writing to a text file with the current count and updating if the time has crossed midnight since the last start or close means that the operation WILL be stable.

    Personally if I were writing this I would be using the registry hives (HKLM) with a real application rather than a script.
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #5
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Try this:-

    Code:
    <!DOCTYPE html>
    <head>
    </head>
    <body>
    
    <span id = "message"></span>
    
    <script type = "text/javascript">
    
    var startDate = new Date(2014,4,30);  // year,month,day - months in Javascript are 0-11
    var today = new Date();  // the date each day
    var oneday = 1000 * 60 * 60 * 24;  // The number of milliseconds in one day
    var dx_ms = startDate.getTime();  // Convert both dates to milliseconds
    var dy_ms = today.getTime();
    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(dy_ms - dx_ms);
    // Convert back to days
    var diff =  Math.floor(difference_ms/oneday);
    document.getElementById("message").innerHTML = "The number of days is without an accident is  " + diff;
    </script>
    
    </body>
    </html>

    The simplest way to rest the counter to 0 is to change the startDate value to the now current date. You could use a cookie, but the snag of that is if the cookie is deleted then the code reverts to the original start date.

    @chrishurst - why do you think that a file is required?


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    That's perfect for what it is going to be used for, I just need to add some CSS to pretty it up and away we go. I might need to put in some other content and rotate it, sort of like a Powerpoint presentation, but that is beyond this. I'm not to worried about having to reset it, editing the script is not a big deal. I will just put lots of comments in if someone else needs to do it.

    Thanks so much for the help and comments.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by Beauford View Post
    That's perfect for what it is going to be used for, I just need to add some CSS to pretty it up and away we go. I might need to put in some other content and rotate it, sort of like a Powerpoint presentation, but that is beyond this. I'm not to worried about having to reset it, editing the script is not a big deal. I will just put lots of comments in if someone else needs to do it.

    Thanks so much for the help and comments.
    Glad to be of assistance.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

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