Changing global variable? #1511
-
Hi all, // initialize response window
var adaptive_response_window = 1230
// try to change variable using function call
function myfunc() {
adaptive_response_window = 200
}
var update_adaptive_window = {
type: 'call-function',
func: myfunc
}
// try to change variable on trial finish
var trial = {
type: 'html-keyboard-response',
//response_ends_trial: true,
stimulus: 'hi',
on_finish: function(){
adaptive_response_window = 200}
}
// display response window to see if it changed
var debugging = {
type: 'instructions',
pages: [
adaptive_response_window
],
key_forward: ' '
}
jsPsych.init({
timeline: [update_adaptive_window, trial, debugging],
}); Thanks a lot for any help anyone could provide! And please let me know if this isn't the right place to ask. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @TomRui! Your functions that update the variable's value look like they should work. The problem might just be that you're not seeing the updated value because the debugging trial doesn't use a dynamic parameter for the page content, so it'll only ever show the initial value. // initialize response window
var adaptive_response_window = 1230
// try to change variable using function call
function myfunc() {
adaptive_response_window += 200;
console.log('myfunc value: ', adaptive_response_window)
}
var update_adaptive_window = {
type: 'call-function',
func: myfunc
}
// try to change variable on trial finish
var trial = {
type: 'html-keyboard-response',
//response_ends_trial: true,
stimulus: 'hi',
on_finish: function(){
adaptive_response_window += 200;
console.log('on finish value: ',adaptive_response_window);
}
}
// display response window to see if it changed
var debugging = {
type: 'html-keyboard-response',
stimulus: function () {
return adaptive_response_window;
}
}
jsPsych.init({
timeline: [update_adaptive_window, trial, debugging],
}); |
Beta Was this translation helpful? Give feedback.
Hi @TomRui! Your functions that update the variable's value look like they should work. The problem might just be that you're not seeing the updated value because the debugging trial doesn't use a dynamic parameter for the page content, so it'll only ever show the initial value.
It might be easier to set up your debugging trial with the html-keyboard-response plugin. Another thing you can do is use
console.log
in your functions to see when they're called and check variable values. Try this: