Passing data to the function in a call-function object #1817
-
Hi, I have tried just about everything I can think of including a whole bunch of searching for a solution, but I can't seem to work this out... I have a call-function in my script that seems happy enough to change the background image and start up a sound recording when I call it. That code looks like this...
However, when I try to add a variabel into the data and use that variable in the function it generates a blank screen. That code looks like this...
Note that the additional bits of code are highliughted in bold. I have played around with it a bit and it looks like the only bit of code that it doesn't like is where I actually refer to the data in the function (var context = data.Context). Otherwise, it seems to work OK. Eventually, what I would like is to replace the 'context' variable in the function with the MiniBlockDat.Context variable so that the background and sound change on every trial (note that this piece of code is housed within two for loops that increment the variables bl and mb respectively). As I said, I have tried to do this a whole load of different ways, but the critical thing that keeps kicking me out seems to be when I use the data that I pass to the function within the function. Any advice appreciated, Cai |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hi Cai, I think one problem here is that the call-function's Another problem in your case is that you're looping through different One approach is to use A second approach is to use immediately-invoked function expressions (IIFEs, see the 2nd code block in this example). This is an anonymous function that is called immediately, along with any information you need to get from the current for loop context, and returns a function definition that uses the values that you passed in as arguments. In your case I think it would look like this: var context = MiniBlockDat.Context[bl][mb]; // this value changes during your for loop
var context_start = {
type: 'call-function',
func: (function(curr_context) {
return function(){
audio = new Audio()
audio.src = "aud/" + curr_context + ".mp3"
audio.loop = true
audio.play()
document.body.style.backgroundImage = "url('img/" + context + ".jpg')"
document.body.style.backgroundSize = "100% 100%"
};
}(context)); // the IIFE passes in the current value of this context variable, and returns a function definition that uses the value
}
timeline.push(context_start); A third approach is to make the Finally, there's a similar approach to the one above that would work if the current var curr_trial = 0;
var context_start = {
type: 'call-function',
func: function() {
var context = all_contexts[curr_trial]; // get the appropriate value based on the current trial number
// ... set up audio etc.
}
};
var trial = {
// other parameters ...
on_finish: function(data) {
curr_trial++; // increment trial count after each trial
}
}; I hope this helps. Let us know if any of these approaches work for you. |
Beta Was this translation helpful? Give feedback.
Hi Cai, I think one problem here is that the call-function's
func
parameter isn't passed the trial data as an argument, as is the case with theon_finish
parameter. Instead, when the call-function is asynchronous, then "the first argument passed to func will be a callback that you should call when the async operation is complete" - from the call-function parameter descriptions in the docs. This example from the docs shows how the argument to thefunc
function parameter should be used.Another problem in your case is that you're looping through different
context
values when the page first loads, but then you need those different values to be "piped in" to the function definitions that are …