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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Playing Script on a certain time

    Suppose I have some function ... myFunction();
    And I want it to be executed from 3am - 11pm but not at any other time
    So far what i had done is along the line of

    Code:
    for(i=0;i<99999;i++)
    {
    var time = new Date();
    var hours = time.getHours();
    if (hours>=3 && hours<=23){
    myFunction();
    }
    }
    What happens so far is that:

    If it is the right time it loops over and over and continually executes the function (which is what I want)

    If it's the wrong time (not 3am-11pm) it loops once, doesn't execute the functoin and then the script finishes (whereas I want it to continue looping until the right time comes along)

    If its the right time and then it moves to the wrong time it stops looping, whereas i want it to not execute the function but continue looping until the right time comes around again.


    So the overall affect is that at the given times of day the script plays and when its not the correct time it doesnt and then when it is the right time again it starts playing again, seemlessly and continually
    Last edited by Maestro; 07-14-2010 at 05:49 PM.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    727
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You could pass the function and its time limits to a scheduler.

    If the current local time is after the start and before the end limit, return the function.

    Otherwise, find the next start time.
    If the current time is greater than the end limit,
    the next start time will be the next day.

    When you have the next start time, subtract the current time from it to get the milliseconds difference.

    Use the difference to set a timeout, the function will be called after the correct interval
    (when the current time reaches the next start time).

    If you pass the start and end limits as arrays, you can schedule to the minute.


    Code:
    function timeSlot(fun, start, end){
    	var diff, playstart= new Date(),
    	playend= new Date(), now= new Date();
    
    	for(var i= 0; i<4; i++){
    		if(start[i]== undefined) start[i]= 0;
    		if(end[i]== undefined) end[i]= 0;
    	}
    	playstart.setHours.apply(playstart, start);
    	playend.setHours.apply(playend, end);
    
    	if(playstart<= now && playend>= now) return fun();
    
    	if(playend< now && playstart.getDate()== now.getDate()){
    		playstart.setDate(playstart.getDate()+1);
    		playEnd.setDate(playend.getDate()+1);
    	}
    	setTimeout(diff,playstart- now);
    }
    //test function
    Code:
    function announce(){
    	alert(new Date().toLocaleDateString());
    }
    //test
    timeSlot(announce, [3, 0], [23, 0]);

    If all you need are hours, pass integers:
    Code:
    function timeSlot(fun, start, end){
    	var diff, playstart= new Date(),
    	playend= new Date(), now= new Date();
    	playstart.setHours( start);
    	playend.setHours(end);
    	if(playstart<= now && playend>= now) return fun();
    	if(playend< now && playstart.getDate()== now.getDate()){
    		playstart.setDate(playstart.getDate()+1);
    	}
    	setTimeout(diff,playstart- now);
    }
    //test2
    timeSlot(announce, 3, 23);
    Last edited by mrhoo; 07-14-2010 at 10:00 PM. Reason: comment

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Worked a charm, many thanks!


  •  

    Posting Permissions

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