简体   繁体   English

为什么我会为此功能获得双控制台消息?

[英]Why do I get a double console message for this function?

I have the following function: 我有以下功能:

var checkNameLenght = function(name,nameLenght,allowedLenght,defaultName) {

    var result;

    if(!(nameLenght <= allowedLenght) || !(/[^a-z]/i.test(name))) {
        result = name; 
    }
        else {
            if(opts.debug == true) {
                console.log(name+' is to long or contains special characters / numbers | Please choose a name shorter than '+allowedLenght+' characters or remove any character / number');
            }
            result = defaultName;
        }

    return result;

}

I use it to check the length of a string ( in my case the value of an input ) and if it contains any special characters or numbers. 我用它来检查字符串的长度(在我的例子中是输入的值)以及它是否包含任何特殊字符或数字。

I use it like so: 我这样使用它:

var input = 'Somevalue';

checkNameLenght(input ,input.length,16,'Username');

The only thing is that if the input string contains some of the above conditions than the console will output the message twice. 唯一的事情是,如果input字符串包含上述某些条件,那么控制台将输出两次消息。

Why is that happening ? 为什么会这样?

I have tested it and it works just fine. 我测试了它,它工作得很好。 Are you sure you are not calling the function twice? 你确定你没有两次调用该功能吗? And try to avoid whatever opts.debug does, just use plain old js with if(console) 并尝试避免opts.debug所做的任何opts.debug ,只需使用普通的旧js和if(console)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会在控制台中收到有效的 function 的错误消息? - Why do I get an error message in console for a function that works? 为什么我得到控制台错误 findIndex() is not a function - Why do I get console error findIndex() is not a function 为什么我得到一条消息说forEach不是函数? - Why do I get a message saying forEach is not a function? 为什么我在控制台中收到一条错误消息,指出 getStoredQuests.push 不是 Object.addQuestionOnLocalStorage 的 function - Why do i receive an error message in the console that getStoredQuests.push is not a function at Object.addQuestionOnLocalStorage 为什么我在JavaScript的控制台中无法定义 - why do i get undefined in the console in JavaScript 尝试进行申请时,为什么会收到一条消息,指出“对象不是函数”? - Why do I get a message saying “Object is not a function” when I try to do an apply? 为什么我收到此错误消息? - Why do I get this error message? 如果我 console.log(i) 为什么我会得到这个答案? - If I console.log(i) why do I get this answer? JAVASCRIPT-&gt;为什么会收到错误消息“ Uncaught TypeError:number not a function”? - JAVASCRIPT --> Why do I get the error message “Uncaught TypeError: number is not a function ”? 如果我在控制台中运行它,为什么会出现语法错误? {} === {} - Why do I get a syntax error if I run this in the console? {} === {}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM