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 JavaScript and the DOM (Retiring) Responding to User Interaction Delaying Execution with setTimeout()

Justin Houghton
Justin Houghton
4,560 Points

Error using setTimeout () in Workspace console

I know there's probably a simple explanation and I missed a typo or something! (or maybe window.setTimeout() only works in the browser console?) Please see below for the function in question.

window.setTimeout((something) => {
console.log(something);  
}, 3000, 'Greetings, everyone!');

When opening the javascript console on chrome - I am able to see the output of the function, but when I try to see the output of the function in the workspace console, (entering node temp.js in the console) I get the following error.

ReferenceError: window is not defined
at Object.<anonymous> (/home/treehouse/wor kspace/temp.js:1:63)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.j s:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3

2 Answers

ywang04
ywang04
6,762 Points

The error is shown clearly. ReferenceError: window is not defined That means window.setTimeout() only works in the browser console instead of node environment.

Michael Brown
Michael Brown
12,406 Points

I believe that "window" can only be used in the console after the page has fully been loaded.

See here: https://developer.mozilla.org/en-US/docs/Web/API/Window

What is it you're trying to accomplish with the setTimeout() function?

Justin Houghton
Justin Houghton
4,560 Points

The beginning of the lesson was an explanation and example of passing a function within a function. And then an explanation of window.setTimeout so that the string 'Greetings, everyone!' appeared in the console after a few seconds.