Waiting4CodeOh

Return to Code Snippets =Code to generate a "Do Something" message after a period of inactivity= = = See note about Clearing Intervals and Listeners for details on tidying up after scene is finished

Code to be copied and pasted into a scene, and modified to fit the current situation
code format="actionscript" // input1_txt is a text field that already exists, and should be waiting for some type of input // feedback1_txt is a text field that already exists, and will be used to display feedback output // check_btn is a check button that already exists, that when pressed evaluates input1_txt for correctness

var time_counter:Number = 0; var attempts_counter = 0;

if (intervalID != null) {// delete old interval clearInterval(intervalID); } intervalID = setInterval(this, "updateTime", 1000); // call the function updateTime once every second SetIntervalsArray.push(intervalID); // register intervalID with array so that tidy up can happen after scene

function updateTime { var WaitTime = 30; time_counter++; if (time_counter >= (WaitTime - 1) && time_counter < WaitTime ) { feedback1_txt.htmlText = ""; // blink effect }   if (time_counter>WaitTime) { feedback1_txt._visible = true;

if (Number(input1_txt.text)>0) { feedback1_txt.htmlText = "Press the Check button."; } else { feedback1_txt.htmlText = "Enter a number into the box, then press Check."; }       Selection.setFocus("input1_txt"); Selection.setSelection(0,input1_txt.length); time_counter = WaitTime / 2; } }

input1_txt.onChanged = check_btn.onPress=function { // Reset the timer, and clear away the "Enter something" message feedback1_txt.htmlText = ""; time_counter = 0; };

check_btn.onRelease = function { time_counter = 0; feedback1_txt.text = ""; attempts_counter++; // increment the number of attempts

if (attempts_counter<6) { // adjust the attempts_counter comparison number above if needed if (Number(input1_txt.text) == 18) { // change == 18 to == Your Correct Answer feedback1_txt._visible = true; feedback1_txt.htmlText = "Correct Enter Correct Message - Positive Reinforcement statement here"; input1_txt.type = 'dynamic'; check_btn._visible = false; showcontinue; clearInterval(intervalID); // clear the interval - stop the process } else { feedback1_txt._visible = true; feedback1_txt.htmlText = "Incorrect Enter Try Again message here."; Selection.setFocus("input1_txt"); Selection.setSelection(0,input1_txt.length); }   } else if (attempts_counter=6) { // adjust the 6 above and the 18 below to YOUR values for # of attempts and correct answer input1_txt.type = 'dynamic'; input1_txt.htmlText = "18"; check_btn._visible = false; feedback1_txt._visible = true; feedback1_txt.htmlText = "Enter Here's the Correct Answer but You Didn't Get It Yourself message here"; showcontinue; clearInterval(intervalID); // clear the interval - stop the process } };

//It is possible that other interactions may effect the elements //for example .... in the check_btn routine, I added this: check_btn.onRelease = function { draghere_txt._visible = false; time_counter = 0; intro_txt.htmlText = "Build Position 0 of the pattern by dragging circles to the space above the zero."; } code

Code to be copied and pasted into a scene, and modified to fit the situation with multiple input fields
code format="actionscript" // input1_txt, input2_txt, input3_txt are text fields that already exists, and should be waiting for some type of input // feedback1_txt is a text field that already exists, and will be used to display feedback output // check_btn is a check button that already exists, that when pressed evaluates input1_txt for correctness

var time_counter:Number = 0; var attempts_counter = 0;

if (intervalID != null) {// delete old interval clearInterval(intervalID); } intervalID = setInterval(this, "updateTime", 1000); // call the function updateTime once every second SetIntervalsArray.push(intervalID); // register intervalID with array so that tidy up can happen after scene function updateTime { var WaitTime = 30; time_counter++; if (time_counter >= (WaitTime - 1) && time_counter < WaitTime ) { feedback1_txt.htmlText = ""; // blink effect }   if (time_counter>WaitTime) { feedback1_txt._visible = true;

if (Number(input1_txt.text)>0 && Number(input2_txt.text)>0 && Number(input3_txt.text)>0 ) { feedback1_txt.htmlText = "Press the Check button."; } else { feedback1_txt.htmlText = "Enter a number in each input box, then press Check."; }       var target = ""; if ( input1_txt.type == "input" ) { target = "input1_txt"; } else if (input2_txt.type == "input" ) { target = "input2_txt"; } else if (input3_txt.type == "input" ) { target = "input3_txt"; }       Selection.setFocus(this[target]); Selection.setSelection(0,this[target].length); time_counter = WaitTime / 2; } }

input1_txt.onChanged = input2_txt.onChanged = input3_txt.onChanged = check_btn.onPress=function { // Reset the timer, and clear away the "Enter something" message feedback1_txt.htmlText = ""; time_counter = 0; };

check_btn.onRelease = function { time_counter = 0; feedback1_txt.text = ""; attempts_counter++; // increment the number of attempts

if (attempts_counter<6) { // adjust the attempts_counter comparison number above if needed if (Number(input1_txt.text) == 18 && Number(input2_txt.text) == 19 && Number(input3_txt.text) == 20) { // change == 18, 19, 20 to == Your Correct Answer feedback1_txt._visible = true; feedback1_txt.htmlText = "Correct Enter Correct Message - Positive Reinforcement statement here"; input1_txt.type = 'dynamic'; check_btn._visible = false; showcontinue; clearInterval(intervalID); // clear the interval - stop the process } else { // further customization will be needed here to take into account all possibilities of incorrect answers feedback1_txt._visible = true; feedback1_txt.htmlText = "Incorrect Enter Try Again message here."; Selection.setFocus("input1_txt"); Selection.setSelection(0,input1_txt.length); }   } else if (attempts_counter=6) { // adjust the 6 above and the 18 below to YOUR values for # of attempts and correct answer input1_txt.type = 'dynamic'; input1_txt.htmlText = "18"; input2_txt.type = 'dynamic'; input2_txt.htmlText = "19"; input3_txt.type = 'dynamic'; input3_txt.htmlText = "20"; check_btn._visible = false; feedback1_txt._visible = true; feedback1_txt.htmlText = "Enter Here's the Correct Answer but You Didn't Get It Yourself message here"; showcontinue; clearInterval(intervalID); // clear the interval - stop the process } }; code