简体   繁体   中英

Javascript keypress into keypress

I wrote a function addMoney(), which calls by clicking Enter into input field:

$('body').on('keypress', 'input[type=text]', function(e){
     if(e.keyCode==13){
         var val = $('input[type=text]').val();
         switch(val) {
             case '-help': 
                 SeeComands();   
                 break
             case '-copyright':
                 SeeAuthors();
                 AddNewLine();
                 break
             case '-addmoney':
                 addMoney();
                 break
             default: 
                 SaveCommand();
                 Animal('I see nothing here, try smthing else',function(){AddNewLine();})
         } 
     }

This function creates a new input field, clicking an Enter in which starts a finding of regular expression in string, which was handed over last input:

function addMoney() {
SaveCommand();

Animal('E', function(){//интсрукция ко вводу  Enter the number of banknotes introduced through the gap
    Animal('U',function(){ //Use a template <count of banknotes>x<nominal of banknotes>
        AddNewLine();
        $('body').on('keypress', 'input[type=text]', function(e){//Checking the input string
             if(e.keyCode==13){
                 var vali = $('input[type=text]').last().val();
                 console.log('val: ', vali);
                 SaveCommand();
                 var reg100 = /([1-9]{1,})(?=x100)/g; //RegExp to find the amount of the nominal value of 100 bills
                 var reg200 = /([1-9]{1,})(?=x200)/g; //RegExp to find the amount of the nominal value of 200 bills
                 var reg500 = /([1-9]{1,})(?=x500)/g; //RegExp to find the amount of the nominal value of 500 bills
                 var reg1000 = /([1-9]{1,})(?=x1000)/g; //RegExp to find the amount of the nominal value of 1000 bills
                 var reg5000 = /([1-9]{1,})(?=x5000)/g; //RegExp to find the amount of the nominal value of 5000 bills
                 var result100 = vali.match(reg100);
                 var result200 = vali.match(reg200);
                 var result500 = vali.match(reg500);
                 var result1000 = vali.match(reg1000);
                 var result5000 = vali.match(reg5000);
                 console.log('100:', result100, '200:', result200, '500:', result500, '1000:', result1000);
             };
        });
    });
});
};

But brake of the switch is working earlier, till AddMoney() is working. As Result vali declared in:

var vali = $('input[type=text]').last().val();

is undefined and as result i have not a result of addMoney(), but have a default switch result: Animation "I see nothing here, try smthing else" I think it can b due function Animal():

function Animal(string, callback) {
    var a = ''; //The variable which will be entered character by character string
    var i= 0;//Counter of a letters
    var p = document.createElement('p');
    $('body').append(p);

    Anima();
    function Anima() {//Animation Function  
        a=a+string[i];
        i++;
        $('p').last().text(a);
        var timer = setTimeout(Anima, 100);
        if(i==string.length){
            clearTimeout(timer);
            callback();//For running in order
        };
    };
};

...due function Anima(), which runs as recursion through SetTimeOut()

i did it by done a new function DefaultCase():

function addMoney(str){
var reg100 = /([0-9]+)(?=x100)/g; //Регулярка для поиска количества купюр номинала 100
var reg200 = /([0-9]+)(?=x200)/g; //Регулярка для поиска количества купюр номинала 200
var reg500 = /([0-9]+)(?=x500$|x500\s)/g; //Регулярка для поиска количества купюр номинала 500
var reg1000 = /([0-9]+)(?=x1000)/g; //Регулярка для поиска количества купюр номинала 1000
var reg5000 = /([0-9]+)(?=x5000)/g; //Регулярка для поиска количества купюр номинала 5000
var result100 = str.match(reg100);
var result200 = str.match(reg200);
var result500 = str.match(reg500);
var result1000 = str.match(reg1000);
var result5000 = str.match(reg5000);

if((result100 == 0) && (result200 == 0) && (result500 == 0) && (result1000 == 0) && (result5000 == 0)){
    Animal('Enter the amount u need to add. Check the errors', function(){
        AddNewLine();
    });
} else {
    if ((valFree(100)<result100) || (valFree(200)<result200) || (valFree(500)<result500) || (valFree(1000)<result1000) || (valFree(5000)<result5000)) {
        Animal('U r tryig to add too many banknotes. Check free place in ATM', function(){
            AddNewLine();
        });
    } else {
        var resstring = 'success: '+str;
        Animal(resstring, function(){
            resstring = '100:+['+result100+']bills 200:+['+result200+']bills 500:+['+result500+']bills 1000:+['+result1000+']bills 5000:+'+result5000;
            Animal(resstring, function() {
                AddNewLine();
            })
        })};
    }
};

//Функция изменения имени
function ChangeName(str) {
if(str.indexOf('@')<(str.length-1)){
    name = str.match(/@(\w+)/)[1];//Регулярка поиска имени
    AddNewLine();
} else {
    Animal('Enter Valid Name.', function(){
        AddNewLine();
    }); 
};
};

//Функция Стандартного Default case в switch
function defaultCase(str) {
SaveCommand()
if(str.match(/-addmoney/)) {
    addMoney(str);
} else {
    if(str.match(/-name @/)){
        ChangeName(str);
    } else {
    Animal('I see nothing here, try smthing else',function(){AddNewLine();})

    }
}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM