Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Derek Lam
Derek Lam
1,012 Points

Javascript not running in Chrome extension

I have installed Run Javascript extension in Chrome. However, i put the script, to refresh a page at certain time, it sometimes run and sometimes don't. What would be the problem?

the code is at follow:

function refreshAt(hours, minutes, seconds) { var now = new Date(); var then = new Date();

if(now.getHours() > hours ||
   (now.getHours() == hours && now.getMinutes() > minutes) ||
    now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
    then.setDate(now.getDate() + 1);
}
then.setHours(hours);
then.setMinutes(minutes);
then.setSeconds(seconds);

var timeout = (then.getTime() - now.getTime());
setTimeout(function() { window.location.reload(true); }, timeout);

}

refreshAt(09,29,55); refreshAt(09,29,55.50); refreshAt(09,29,56); refreshAt(09,29,56.50);

1 Answer

Mark Casavantes
Mark Casavantes
2,880 Points

Hello Derek,

In the code you posted, "now" is not defined. I am not sure if you just posted part of your code.

Consider using codepen.io to run your HTML, CSS and JavaScript. It is free and it will work.

I hope I have helped you.