[英]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.