countdown and response at the same time - OSweb
Hi everyone,
I don’t know if it’s possible, but I wonder if there’s a way to ask a participant something and in the meantime put a countdown in a corner to be seen by him.
In python it might be easy to implement but with inline_javascript I’m experiencing some problems...
Thanks for the advice and answers you will give me!
Cheers
Sara
Comments
Hi @SER,
Can you be more specific? What do you mean by "ask a participant something"? A simple key press? text input?
Cheers,
Fabrice.
Hi @Fab,
the request is a simple mathematical question; the participant must answer a mathematical operation that is presented to him.
So Basically the response is a number (or more than one) and the participant must press 'return' to submit the answer.
Cheers,
Sara
Hi @SER,
Thanks for the information. You haven't specified how you collect the response. From your description, it sounds as if you might be using a form_text_input object as opposed to collecting and appending multiple key strokes. If you're using the form_text_input object, you won't be able to display anything else on the screen at the same time.
The solution to your problem has to be achieved through coding (in Python in this case). Are you familair with Python? If not, I recommend you take the time to train yourself a little so that you can understand the following code (my knowledge of Python is basic but it's not too complicated to learn and there is a lot of information out there on the internet):
# Import the necessary modules from openexp.canvas import canvas from openexp.keyboard import keyboard # Set the countdown duration countdown_duration = 10 # Countdown duration in seconds start_time = self.clock.time() # Create a canvas to display the problem and countdown timer disp = canvas(exp) # Initialize the keyboard kb = keyboard(exp) # Function to update and show the display def update_display(response_str): elapsed_time = (self.clock.time() - start_time) / 1000.0 remaining_time = max(0, countdown_duration - elapsed_time) disp.clear() disp.text(var.problem, center=True) disp.text(f'Time left: {remaining_time:.1f} seconds', x=0, y=-200) disp.text(f'Your answer: {response_str}', x=0, y=200) disp.show() # Display the initial problem and countdown timer response_str = '' update_display(response_str) # Wait for response or timeout timeout = True response_time = None while (self.clock.time() - start_time) < (countdown_duration * 1000): key, rt = kb.get_key(timeout=10) if key: timeout = False print(f"Key pressed: {key}") # Print key press to console if key == 'return': response_time = self.clock.time() - start_time break elif key == 'backspace': response_str = response_str[:-1] elif key in '0123456789': response_str += key update_display(response_str) # Print the response and correct answer for debugging print(f"Participant's response: {response_str}") # Print response to console print(f"Correct response: {var.correct_response}") # Print correct response to console # Convert response_str to the appropriate type if needed try: response_value = float(response_str) except ValueError: response_value = response_str # Keep as string if conversion fails # Check if the response is correct if timeout: feedback_text = "Too slow!" feedback_color = "white" elif response_value == var.correct_response: correct = 1 feedback_text = "Correct" feedback_color = "green" print("Response is correct.") # Print correctness to console else: correct = 0 feedback_text = "Incorrect" feedback_color = "red" print("Response is incorrect.") # Print correctness to console # Set the correct variable if not timeout if not timeout: var.correct = correct # Set the response_time variable if response_time is None: var.response_time = "N/A" # If timeout, no response time # Create a new canvas for feedback feedback_canvas = canvas(exp) feedback_canvas.text(feedback_text, center=True, color=feedback_color, font_size=30) feedback_canvas.show() # Pause to show feedback for a short duration self.experiment.clock.sleep(2000) # Show feedback for 2000 milliseconds (2 seconds)Below is a step-by-step description of the logic and flow of the code. Hopefully it will make sense to you. From this, you should be able to modify the code if needed (for example to change the position or size of the text being displayed).
canvasandkeyboardmodules from theopenexplibrary. These modules are used to display text on the screen and to capture keyboard input, respectively.self.clock.time().canvasobject nameddispis created to display the math problem and the countdown timer.keyboardobject namedkbis created to capture the participant's keyboard input.update_displayis defined to update the display with the math problem, remaining time, and the participant's current input (response_str).update_displaywith an empty response string.response_str) is converted to a number if possible. If the conversion fails, the response is kept as a string.var.correct_response).correctvariable is set to 1 if the response is correct and 0 if it is incorrect, but only if the participant did not timeout.response_timevariable is set to the time it took for the participant to respond or "N/A" if there was a timeout.canvasobject namedfeedback_canvasis created to display the feedback message.Here is the task:
Hope this helps.
Best,
Fabrice.
Hi @Fab,
Thank you so much for the code.
I know that in python it is very easy to get the answer but I can’t do the same in Java (with OSweb).
I can’t set the keyboard in the code and this behavior causes me some problems to display both the countdown and the subject’s response at the same time.
Cheers
Sara
Hi @SER,
The code I sent you is Javascript and the example I sent you works in OSWeb.
I'm not sure whether your task is now working. If not, let me know what the issue is exactly, or better still, upload a stripped down version of your task.
Best,
Fabrice.
Hi @Fab,
sorry but I cannot run your code on OSweb.
It seems to me that the code you send is written in Python...
Cheers
Sara