SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hidden Alarm Clock?

    I have a page that will be displayed on a monitor continuously, 24 hours a day, 7days a week, 365 yada yada yada... I need to create a script that monitors system time and refreshes the browser every morning at 5:00am est. Can someone point me in the right direction?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    If the current time is before the target time then you just wait for the difference, targetTime - time, and if the time is after the target time you wait for the time between midnight and now, plus the target time.

    That can be optimised to using targetTime - time in both occasions, and adding a day if the targetTime has already passed.

    The scope of this function has been broadened to be more useful, in that you can pass it any time and a function to run.

    Code Javascript:
    function at(args, fn) {
    	console.log(args.minutes);
    	this.hours = (args.hours || 0) * 60 * 60 * 1000;
    	this.minutes = (args.minutes || 0) * 60 * 1000;
    	this.seconds = (args.seconds || 0) * 1000;
    	this.milliseconds = args.milliseconds || 0;
    	var targetTime = this.hours + this.minutes + this.seconds + this.milliseconds;
    	// get difference
    	var now = new Date();
    	var difference = 0;
    	time = now.getHours() * 60 * 60 * 1000;
    	time += now.getMinutes() * 60 * 1000;
    	time += now.getSeconds() * 1000;
    	difference = targetTime - time;
    	if (difference < 0) {
    		difference += 24 * 60 * 60 * 1000;
    	}
    	setTimeout(fn, difference);
    }
    function refreshWindow() {
    	console.log('refresh');
    	window.location.href = unescape(window.location.pathname);
    }
    at({hours: 0, minutes: 13}, refreshWindow);
    Last edited by paul_wilkins; Jan 24, 2008 at 06:21.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Here's a reworking of the above code that I'm much more happier with.

    Code Javascript:
    function refreshWindow() {
    	window.location.href = unescape(window.location.pathname);
    }
    function at(args, fn) {
    	var now = new Date();
    	var then = new Date();
    	then.setHours(args.hours || 0);
    	then.setMinutes(args.minutes || 0);
    	then.setSeconds(args.seconds || 0);
    	then.setMilliseconds(args.milliseconds || 0);
    	// Ensure the target time is in the future
    	while (then < now) {
    		then.setDate(then.getDate() + 1);
    	}
    	setTimeout(fn, then - now);
    }
    at({hours: 5}, refreshWindow);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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