javascript - How to force loop to wait until user press submit button? -
i have simple function checks if entered pin code valid. don't know how force for-loop wait until enter code again check again it's validity.
how should - type pin code, click ok button , checks whether it's correct (if is, can see account menu; if it's not have type again , have 2 chances left). code fails, because pin when code wrong program should wait until type new code , press ok button again.
tried settimeout(), callback(), doesn't work. have - function for-loop runs 3 times (as suppose to, not instantly) without giving chance correct pin code.
that's whole, unfinished yet, code: http://jsfiddle.net/j1yz0zuj/
only function for-loop, checks validity of pin code:
var submitkey = function(callback) { console.log("digit status" + digitstatus); if (digitstatus == 0) { correctpin = 1234; var onscreen = document.getelementbyid("screen"); (i=0; i<3; i++) { if (onscreen.innerhtml.slice(15, onscreen.innerhtml.length) == correctpin) { settimeout(accountmenu, 1250); //break; } else { onscreen.innerhtml += "<br> błędny kod pin! wpisz pin ponownie. <br> pozostało prób: " + (2-i); callback(); //cardinserted = function(function(){console.log("ponowne wpisanie pinu");}); } if (i=2) console.log("blokada"); } } else if (digitstatus == 1) { } }
your approach wrong. should not make user wait!!! need 2 more variables @ top of programm pincount=0 , pininputallowed. increase pincount in submit key function 1 , check if pincount<3.
here corrected version of code.
http://jsfiddle.net/kvsx0kkx/16/
var pincount=0, pinallowed=true; var submitkey = function() { console.log("digit status" + digitstatus); if (digitstatus == 0) { correctpin = 1234; var onscreen = document.getelementbyid("screen"); pincount++; if(pincount >= 3) { pinallowed = false; onscreen.innerhtml = "<br>blokada"; } if(pinallowed){ if (onscreen.innerhtml.slice(15, onscreen.innerhtml.length) == correctpin) { settimeout(accountmenu, 1250); //break; } else { onscreen.innerhtml += "<br> błędny kod pin! wpisz pin ponownie. <br> pozostało prób: " + (3-pincount); inputlength = 0; document.getelementbyid("screen").innerhtml += "<br>wpisz kod pin: "; //callback(); //cardinserted = function(function(){console.log("ponowne wpisanie pinu");}); } } } else if (digitstatus == 1) { } }
you need create more variables control machine. add/delete digit function had conditions badly written , worked if text on screen short enough.
var inputlength = 0; adddigit = function(digit){ //numkeyvalue = numkeyvalue instanceof mouseevent ? this.value : numkeyvalue;{ if (inputlength < pinlength) { onscreen.innerhtml += this.value; inputlength++; } //if (onscreen.innerhtml == 1234) console.log("pin został wprowadzony"); }, deldigit = function(){ if (inputlength >= 0) { onscreen.innerhtml = onscreen.innerhtml.slice(0, -1); inputlength--; } };
if want empty screen @ moment can insert onscreen.innerhtml = '';
anywhere ps: exercise , nice automat made there.
Comments
Post a Comment