$(function () { 'use strict'; var data = []; /* Reference DOM elements */ var go = $('#go'), goError = $('#go-error'), nogo = $('#nogo'), nogoError = $('#nogo-error'); /* Task flow variables */ var counter = 0, currentTrial = null, correct = null, keyPressed = false, goErrorTimeout, nogoErrorTimeout, timer = void 0, time_elapsed = void 0; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Timer = function () { // begin timer, end timer, and difference time. function Timer() { _classCallCheck(this, Timer); this.start_time = new Date().getTime(); } _createClass(Timer, [{ key: 'start', value: function start() { this.start_time = new Date().getTime(); } }, { key: 'get_elapsed', value: function get_elapsed() { return new Date().getTime() - this.start_time; } }]); return Timer; }(); goNoGo(); $(this).on('keydown', function (e) { if (e.which === 32) { e.preventDefault(); if(!keyPressed) { time_elapsed = timer.get_elapsed(); answerCheck(); } } }); $('#gonogo-bar').on('click', function () { if(!keyPressed) { time_elapsed = timer.get_elapsed(); answerCheck(); } }); function goNoGo() { counter ++; currentTrial = trials[counter-1]; go.hide(); nogo.hide(); setTimeout(function () { keyPressed = false; timer = new Timer(); $('#'+currentTrial).show(); }, 300); if(currentTrial === 'go') { goErrorTimeout = setTimeout(function () { correct = false; console.log(data); go.hide(); keyPressed = true; goError.show(); setTimeout(function () { goError.hide(); fillData(counter, currentTrial, correct, 2000); goNoGo() }, 2000) }, 2000) } else { nogoErrorTimeout = setTimeout(function () { correct = true; fillData(counter, currentTrial, correct, 2000); console.log(data); nogo.hide(); goNoGo(); }, 2000) } } function answerCheck() { keyPressed = true; if (currentTrial === 'nogo'){ correct = false; clearTimeout(nogoErrorTimeout); nogo.hide(); nogoError.show(); setTimeout(function () { nogoError.hide(); fillData(counter, currentTrial, correct, time_elapsed); console.log(data); goNoGo() }, 2000) } else { correct = true; clearTimeout(goErrorTimeout); fillData(counter, currentTrial, correct, time_elapsed); console.log(data); goNoGo(); } } function fillData(counter, trial, correct, timeElapsed) { data.push({id: counter, trial: trial, correct: correct, time_elapsed: timeElapsed}); if (data.length === trials.length) { $('input[name=gonogo_trials]').val(JSON.stringify(data)); $('#form').submit() } } });