Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

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.

    Buy Me A Coffee

  • 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

  • FabFab
    edited July 19

    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).

    1. Import Necessary Modules:
      • The script begins by importing the canvas and keyboard modules from the openexp library. These modules are used to display text on the screen and to capture keyboard input, respectively.
    1. Set Countdown Duration:
      • The countdown duration is set to 10 seconds. This is the maximum time allowed for the participant to enter their response.
    1. Initialize the Start Time:
      • The start time of the experiment is recorded using self.clock.time().
    1. Create Display Canvas:
      • A canvas object named disp is created to display the math problem and the countdown timer.
    1. Initialize Keyboard:
      • A keyboard object named kb is created to capture the participant's keyboard input.
    1. Function to Update and Show the Display:
      • A function named update_display is defined to update the display with the math problem, remaining time, and the participant's current input (response_str).
      • Inside this function, the elapsed time is calculated, and the remaining time is displayed. The display is cleared and updated with the problem, remaining time, and the participant's input.
    1. Display Initial Problem and Countdown Timer:
      • The initial problem and countdown timer are displayed by calling update_display with an empty response string.
    1. Wait for Response or Timeout:
      • The script enters a loop that runs until either the participant presses the 'return' key (enter) to submit their answer or the countdown timer reaches zero.
      • Inside the loop, the script checks for key presses. If a key is pressed:
        • If 'return' is pressed, the response time is recorded, and the loop breaks.
        • If 'backspace' is pressed, the last character of the response is deleted.
        • If a digit (0-9) is pressed, it is added to the response string.
      • The display is updated continuously during this loop.
    1. Debugging Information:
      • The participant's response and the correct answer are printed to the console for debugging purposes.
    1. Convert and Check Response:
      • The participant's response (response_str) is converted to a number if possible. If the conversion fails, the response is kept as a string.
      • The response is compared to the correct answer (var.correct_response).
      • If the participant ran out of time without submitting a response, the feedback is set to "Too slow!" in white.
      • If the response is correct, the feedback is "Correct" in green.
      • If the response is incorrect, the feedback is "Incorrect" in red.
    1. Set Experiment Variables:
      • The correct variable is set to 1 if the response is correct and 0 if it is incorrect, but only if the participant did not timeout.
      • The response_time variable is set to the time it took for the participant to respond or "N/A" if there was a timeout.
    1. Display Feedback:
      • A new canvas object named feedback_canvas is created to display the feedback message.
      • The feedback message is displayed in the center of the screen with a font size of 30 and the appropriate color (green for correct, red for incorrect, white for too slow).
    1. Pause to Show Feedback:
      • The script pauses for 2000 milliseconds (2 seconds) to allow the participant to see the feedback before moving on to the next task or trial.

    Here is the task:

    Hope this helps.

    Best,

    Fabrice.

    Buy Me A Coffee

  • 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.

    Buy Me A Coffee

  • 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

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games