Preparatory practical

Basics of jsPsych

The plan for the preparatory practical

The plan is for you to learn enough of the basics of jsPsych so we can jump in to building simple experiments immediately when the course starts. The idea is that you do as much of this as you can on your own (which might be all of it or might be none of it!) and then I can help you figure out stuff that was problematic in the 2 days we have together. If you can read through the materials I point to here and download Visual Studio Code and cyberduck that’ll be fine; if you can also have a go at at least some of the coding problems that’ll be even better (but if you can’t get through them don’t worry).

Our server

Most of the code here will work just running on your computer - each experiment has an html file, you can just open that file in a web browser on your computer (use Chrome). But some of fancier stuff (saving data, playing and recording audio) relies on the code being hosted on a server, a computer somewhere else that is designed to deliver code and files to other computers over the network. When we teach our students at Edinburgh we use a teaching server, jspsychlearning.ppls.ed.ac.uk, where students can upload code and test out experiments, so you’ll see that referred to in some places (e.g. in my colleage Alisdair’s stand-alone tutorial materials). You don’t have access to this server, but Alisdair has set up a temporary equivalent just for this course - it is called tullo.co.uk - so anywhere you see a reference to jspsychlearning.ppls.ed.ac.uk (e.g. in the intro tutorials), just remember that for you this will be tullo.co.uk. I will provide you with your login details by email. If you have problems accessing this then don’t worry, you can make progress on the first few practicals by running code locally, i.e. opening the html file in Chrome on your computer.

Practical tasks

Read the notes and caveats below, then read through (and ideally work through) sections 00, 01, 02, 03, 04 and 05 of Alisdair’s Online Experiments with jsPsych tutorial.

Sections 01-03 will walk you through how to access the teaching server, upload code, edit code in something like Visual Studio Code, give you a (very basic) idea of what a trivial jsPsych “hello world” experiment looks like, and have a bit of an idea of timelines. Sections 04-05 are a bit more advanced but will be useful if you have tme to take a read-through.

Some things to note as you work through the practical.

Solutions to common problems

See below for some solutions to recurring problems. Remember, if you need help getting any of this stuff to work, we can do it in person during the course!

I just get a blank screen

A classic problem is that you try to run your code and just get a blank screen. That means something has gone wrong, but what? The way to start to find out is to open the javascript console and see what it says. Often it will have a line in red saying that it can’t find one or more files, you fix that (e.g. making sure the files are in the right place, see the answers below on directory structure and where to put everything) and that fixes it. Or it will mention a syntax error in one of the files, you fix that, and bingo.

Accessing the javascript console will be essential for finding bugs in your code - this might involve enabling developer tools for your browser, hopefully if you google “how to access javascript console in X” and “how to enable developer tools in X” where X is your browser, you’ll find comprehensible instructions.

I change the code but nothing changes!

You edit your code, re-load the experiment in your browser by clicking the “reload” button, and … nothing changes. You reload again, and still nothing. Are you losing your mind, or is your computer being wilfully disobedient?

There are two reasons this might be happening, neither are due to you losing your mind. One possibility is that your browser is running a cached (stored) version of the code. Browsers are designed to be efficient in what they download over the internet, in order to reduce unnecessary traffic, so often when you click the “reload” button your browser will just re-run the html/javascript code it already downloaded, rather than going back to source, downloading the latest version, and running that. That’s very clever unless the source code has changed, in which case it can be quite frustrating! The solution is to force your browser to re-download the code - on Chrome this is done by hitting Command-Shift-R on a Mac, or Control-Shift-R on Windows.

The other possibility is that you are not actually editing the file that you are viewing in the browser. Maybe you have two versions of the experiment in different folders say, you are editing version 1 but loading version 2 in the browser? This can also happen quite easily if you are using the “Edit with…” option on cyberduck. Behind the scenes, cyberduck makes a local version of the file, you edit that, then when you save your changes it uploads the file to the server for you (which should generate a little pop-up window telling you the new version was uploaded). This is very convenient, but sometimes the version you are editing becomes decoupled from the version on the server - so you are editing the file but those changes are not saved to the server, so you get this confusing behaviour where your changes don’t change anything. The thing to watch out for is the pop-up message you get when you successfully save the file, and or the “Modified” time on the file in cyberduck - if that stops popping up, or the Modified time is wrong, you know you are going to have problems.

Problem connecting to the tullo.co.uk server

Check that you are selecting SFTP in the cyberduck dropdown menu - i.e. for me it looks like the picture below (but you will be using the tullo.co.uk server instead, and with a different username). If you get the error “Connection failed. Connection timed out:connect. The connection attempt was rejected. The server may be down or your network may not be properly configured” this is probably the problem.

cuberduck window

Where do I put my Hello World code on the server?

For the hello world demo, you can just run the code on your own computer, by opening the experiment.html file in a browser. But if you want to test it on the server, you need to copy your code into your public_html folder on the tullo.co.uk server. You want a folder called e.g. “hello_world” which contains experiment.html and the jspsych folder, e.g. something like this:

cuberduck window

How do I run the Hello World code once it’s on the server?

Once you have put your code on the tullo.co.uk server, you might be a bit puzzled about how to open it, and have tried e.g. clicking on the experiment.html file in cyberduck. All that will do is try to download the file back to your computer - cyberduck is for moving files about. The way to access your experiment once it’s on the server is to open a web browser, e.g. chrome, and put something like https://tullo.co.uk/~USER/hello_world/experiment.html in the address bar, where USER is the username I gave you (e.g. for me this demo is at https://tullo.co.uk/~kenny/hello_world/experiment.html, you can click that link if you want to see it!). Note that this assumes your code is in your public_html folder, in a folder called hello_world - if you are using different directory names on the server, your url might be a little different. Also note the tilde (little squiggle character) in front of your username.

Re-use

All aspects of this work are licensed under a Creative Commons Attribution 4.0 International License.


Course main page

Project maintained by kennysmithed Hosted on GitHub Pages — Theme by mattgraham