Skip to content

Commit

Permalink
first pass at #75 - work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
becky-gilbert committed Feb 26, 2021
1 parent 4a59c86 commit 80f86a4
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions jspsych.js
Expand Up @@ -2515,20 +2515,47 @@ jsPsych.pluginAPI = (function() {
// timeout registration

var timeout_handlers = [];
// for browser compatibility
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

var cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;

module.setTimeout = function(callback, delay){
var handle = setTimeout(callback, delay);
var handle = requestAnimationFrame(function(timestamp) {
// record the start time
var start_time = timestamp;
// setup the next rAF call to check for timeouts and update handle value
handle = requestAnimationFrame(function(timestamp) {
checkForTimeout(timestamp, start_time, callback, delay, handle);
});
});
timeout_handlers.push(handle);
return handle;
}

module.clearAllTimeouts = function(){
for(var i=0;i<timeout_handlers.length; i++){
clearTimeout(timeout_handlers[i]);
cancelAnimationFrame(timeout_handlers[i]);
}
timeout_handlers = [];
}

var checkForTimeouts = function(timestamp, start_time, callback, delay, handle) {
var curr_duration = timestamp - start_time;
// check if the current duration is at least as long as the intended duration
// minus half the typical frame duration (~16 ms). this helps avoid displaying the stimulus
// for one too many frames.
if (curr_duration >= delay - 8) {
callback();
} else {
// setup the next rAF call and update handle value
handle = window.requestAnimationFrame(function(timestamp) {
checkForTimeout(timestamp, start_time, callback, delay, handle);
});
}
}

// video //
var video_buffers = {}
module.getVideoBuffer = function(videoID) {
Expand Down

0 comments on commit 80f86a4

Please sign in to comment.