Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:21:08+00:00 2026-06-11T18:21:08+00:00

Disclaimer: I’m not a real developer, so I’ve probably got some code spaghetti here…

  • 0

Disclaimer: I’m not a real developer, so I’ve probably got some code spaghetti here… AND for now I need to work with what I’ve got, which means I may end up with a duct-tape type fix. 😉

I’m using Javascript and PHP to do the following:

  • flash a pair of images on the screen
  • ‘listen’ for and capture key presses (specific keys to indicate the left or right image)
  • repeat for 10 pairs of images

I’ve done it by having Javascript change the visibility of the divs with the image pairs, and call the event listener, all timed precisely. The ‘capture’ part is done by ‘writing’ the L or R using innerHTML into the form area of the page.

Here’s the problem, I don’t have a way to notice if someone has missed pressing a key during the window of opportunity. So, until the form is submitted at the end of the process, I don’t count the recorded responses.

I’d like it to either notice at the end of the 10 image pairs, and then have the user redo the session, OR notice which image pair(s) got missed and automatically reshow that pair (or pairs).

Here’s the code I’ve got that does the image pair flashing and capturing of the key presses.

<script type="text/javascript">
var randomPairList = <?php echo $_POST['topicNumber']-1; ?>;
function imgChoice(imgPair)
{
  var imgDataR = '<input type="hidden" name="'+imgPair+'[<?php echo $_POST['topicNumber']; ?>]" value="r" />';
  var imgDataL = '<input type="hidden" name="'+imgPair+'[<?php echo $_POST['topicNumber']; ?>]" value="l" />';
  var noData = '<?php $noData = 1; ?>';
  $(document).on('keydown', function(event)
  {
    if (event.keyCode == 67 || event.keyCode == 37)
    {
      document.getElementById(imgPair+'Data').innerHTML = imgDataL;
      $(document).unbind('keydown');
    }
    if (event.keyCode == 77 || event.keyCode == 39)
    {
      document.getElementById(imgPair+'Data').innerHTML = imgDataR;
      $(document).unbind('keydown');
    }
  });
  //div = document.getElementById(imgPair);
}

function stopChoice(imgPair)
{
  $(document).unbind('keydown');
}

function flashImages()
{
  i=500;
  //$('#startTopic').fadeOut(500);
  setTimeout("document.getElementById('fullpd').style.cursor='none';",50);
  setTimeout("document.getElementById('fullpd').style.background='#464646';",500);
  for(x=1;x<imgPairs.length;x++)
  {
    setTimeout("document.getElementById('clickSound').play();",i+2000);
    setTimeout('document.getElementById("'+imgPairs[x]+'").style.display="block";',i+3500)
    setTimeout('imgChoice("'+imgPairs[x]+'");',i+3495)
    setTimeout('document.getElementById("'+imgPairs[x]+'").style.display="none";',i+4000)
    setTimeout('stopChoice("'+imgPairs[x]+'");',i+6000)
    i=i+4000;
  }
  setTimeout("document.getElementById('fullpd').style.background='#eaeaea';",i+1000)
  setTimeout("document.getElementById('fullpd').style.cursor='default';",i)
  setTimeout(function() {$('#endTopic').fadeIn(1000);},i+1000);
}
</script>

So, I’m wondering if there’s either a way to keep a simple count of the event.keyCode such that at the end another function is called to make sure it totals 10 events recorded, and if not restart… or a way to track specifically which imagePair was missed, and then just automatically rerun those pairs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:21:09+00:00Added an answer on June 11, 2026 at 6:21 pm

    I found a way to achieve what I need…

    I added a counter inside of the function that records the key event:

    var randomPairList = <?php echo $_POST['topicNumber']-1; ?>;
    var choicesMade = 0;
    function imgChoice(imgPair)
        {
            var imgDataR = '<input type="hidden" name="'+imgPair+'[<?php echo $_POST['topicNumber']; ?>]" value="r" />';
            var imgDataL = '<input type="hidden" name="'+imgPair+'[<?php echo $_POST['topicNumber']; ?>]" value="l" />';
            var noData = '<?php $noData = 1; ?>';
            $(document).on('keyup', function(event) {
                if (event.keyCode == 67 || event.keyCode == 37) {
                    document.getElementById(imgPair+'Data').innerHTML = imgDataL;
                    $(document).unbind('keyup');
                    choicesMade+=1;
                    document.getElementById('choicesMade').innerHTML = choicesMade;
                    }
                if (event.keyCode == 77 || event.keyCode == 39) {
                    document.getElementById(imgPair+'Data').innerHTML = imgDataR;
                    $(document).unbind('keyup');
                    choicesMade+=1;
                    document.getElementById('choicesMade').innerHTML = choicesMade;
                }
            });
    }
    
    function stopChoice(imgPair)
        {
            $(document).unbind('keyup');
        }
    

    Then I changed the code that displayed the ‘endTopic’ div to have it choose a different div to display if the required number of choices wasn’t reached:

    function flashImages(){
        i=500;
        //$('#startTopic').fadeOut(500);
        setTimeout("document.getElementById('fullpd').style.cursor='none';",50);
        setTimeout("document.getElementById('fullpd').style.background='#464646';",500);
    
        for(x=1;x<=imgPairs.length-1;x++){
            setTimeout("document.getElementById('clickSound').play();",i+2000);
            setTimeout('document.getElementById("'+imgPairs[x]+'").style.display="block";',i+3500)
            setTimeout('imgChoice("'+imgPairs[x]+'");',i+3495)
            setTimeout('document.getElementById("'+imgPairs[x]+'").style.display="none";',i+4000)
            setTimeout('stopChoice("'+imgPairs[x]+'");',i+6005)
            i=i+4000;
            }
        setTimeout("document.getElementById('fullpd').style.background='#eaeaea';",i+1000)
        setTimeout("document.getElementById('fullpd').style.cursor='default';",i)
        setTimeout(function() {if(choicesMade < 10){$('#missedChoices').fadeIn(1000);} else {$('#endTopic').fadeIn(1000);}},i+1000);
        //setTimeout(function() {$('#endTopic').fadeIn(1000);},i+1000);
    }
    

    Then, in the “missedChoices” div, the user is given the message that they have missed some responses, and the button is available to ‘go back’.

    So far, it seems to be working okay! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DISCLAIMER: The following code is not something I would ever use in a real
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial
Disclaimer: this question is driven by my personal curiosity more than an actual need
Disclaimer : I'm quite a novice to RoR, but not with Ruby (if that
Disclaimer : I kept this because some things may be useful to others, however,
(DISCLAIMER: I'm not a programmer, I spend my time on serverfault, I'm just a
Disclaimer : I realize asking Why doesn't my regular expression work is pretty amateur.
Disclaimer : I'm an SSRS n00b, so not too many rotten tomatoes please :)
Disclaimer : I am relatively unfamiliar with the flash build processes, so some/all of
Disclaimer, I'm not a PHP programmer, so you might find this question trivial. That's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.