www.webdeveloper.com
Results 1 to 4 of 4

Thread: Scheduling a function to run at a specified time

  1. #1
    Join Date
    Sep 2008
    Posts
    3

    Scheduling a function to run at a specified time

    Hi,
    I am facing problems while trying to run a particular function at a specified time. I have a simple countdown script which is working fine. Now I want this to be scheduled to run at a future date and time.

    I am enclosing the script below:


    <html>
    <head>
    <script>
    function countdown_clock(fromYear, fromMonth, fromDay, fromHour, fromMinute, fromSecond, toYear, toMonth, toDay, toHour, toMinute, toSecond){

    var currentDate = new Date();
    var fromDate = new Date(fromYear, fromMonth-1, fromDay, fromHour, fromMinute, fromSecond);

    var strCalltoTemp = 'temp(' + currentDate + ',' + fromDate + ')';

    t = setInterval( strCalltoTemp, 1000);

    }

    function temp(currentDate,fromDate){

    if(currentDate.toString() == fromDate.toString()){
    html_code = '<div id="countdown"></div>';
    document.write(html_code);
    countdown(fromYear, fromMonth, fromDay, fromHour, fromMinute, fromSecond, toYear, toMonth, toDay, toHour, toMinute, toSecond);

    }

    }

    function countdown(fromYear, fromMonth, fromDay, fromHour, fromMinute, fromSecond, toYear, toMonth, toDay, toHour, toMinute, toSecond){

    var dateFrom = new Date(fromYear, fromMonth - 1, fromDay, fromHour, fromMinute, fromSecond);
    var dateTo = new Date(toYear, toMonth - 1, toDay, toHour, toMinute, toSecond);

    //Find their difference, and convert that into seconds.
    var timeLeft = Math.round((dateTo.getTime() - dateFrom.getTime()) / 1000);

    if(timeLeft < 0){
    timeLeft = 0;
    }

    decrementCountdown(timeLeft);
    }

    function decrementCountdown(timeLeft){

    //temp
    var tempTimeLeft = timeLeft;

    var days = Math.floor(timeLeft / (60 * 60 * 24));
    timeLeft %= (60 * 60 * 24);

    var hours = Math.floor(timeLeft / (60 * 60));
    timeLeft %= (60 * 60);

    var minutes = Math.floor(timeLeft / 60);
    timeLeft %= 60;

    var seconds = timeLeft;

    var dps = 's';
    var hps = 's';
    var mps = 's';
    var sps = 's';

    //ps is short for plural suffix.
    if(days == 1){
    dps ='';
    }

    if(hours == 1){
    hps ='';
    }

    if(minutes == 1){
    mps ='';
    }

    if(seconds == 1){
    sps ='';
    }

    document.all.countdown.innerHTML = days + ' day' + dps + ' ';
    document.all.countdown.innerHTML += hours + ' hour' + hps + ' ';
    document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and ';
    document.all.countdown.innerHTML += seconds + ' second' + sps;


    //Recursive call, keeps the clock ticking.
    tempTimeLeft--;

    if(tempTimeLeft != 0){
    var strTemp = 'decrementCountdown(' + tempTimeLeft + ')';
    setTimeout(strTemp, 1000);
    }
    }

    </script>
    </head>
    <body>

    <script>

    countdown_clock(2008,10,29,14,0,0,2008,10,29,15,0,0);
    </script>

    </body>
    </html>


    The timer code in decrementCountdown is working fine. Now I need to run this at a date specified in fromDate. I tried various things, but I am not able to figure it out.

    Please let me know if you need more details.

    Any help will be greatly appreciated.

    Regards
    Anurag

  2. #2
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Once the web page closes the script will no longer run. Most people stay on a web page fort seconds, not days and so if you set something to happen more than a minute into the future then the chances of the JavaScript still being loaded when that time is reached is so close to zero that you may as well not bother.
    Stephen

  3. #3
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Try Function.defer but keep in mind what felgall said.
    #define question (2B || !2B)
    HTMLElement and W3C Event Handling in IE
    My JavaScript Library

    Don't PM me about answers to questions. If I don't reply in a thread it's because:
    • You didn't read the message posting guidelines
    • Your code is too unstructured and/or formatted poorly - correcting it is too time consuming
    • I simply don't know the answer

  4. #4
    Join Date
    Sep 2008
    Posts
    3
    Thank you felgall and Dok. I implemented the scheduling logic in PHP instead.

Thread Information

Users Browsing this Thread

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

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
  •  
HTML5 Development Center



Recent Articles