简体   繁体   English

与setTimeout有关的特定textarea行为

[英]Specific textarea behaviour related to setTimeout

what I am trying to do is give the user a textarea (with enabled input) and allow him to write up to 10 chars. 我想做的是给用户一个textarea(具有启用的输入),并允许他最多写10个字符。 Actually, the textarea should ALLOW more chars , not only 10, and the other chars would behave accordingly: 实际上, textarea应该允许更多的字符 ,不仅是10 个字符 ,其他字符也应相应地表现:

There is a setTimeout whenever the user is on the 11ºchar . 每当用户使用11ºchar时,都有一个setTimeout So, I'm writing something there, I reach the char number 10º (the "maximum" allowed of text) and want that other char to be erased AFTER a given time. 所以,我在那儿写东西,我达到了10º的字符数(文本允许的“最大”数),并希望在给定的时间后擦除其他char。

somethinge = 10 chars.
anotherwor = 10 chars.

input: somethingeiefjaiehf
after 5 seconds:
input: somethinge

input: anotherwordtotestthisandseeifitworks
after 5 seconds:
input: anotherwor

2ºTo accomplish this I basically attached a clearTimeout to a global variable: 2º为此, 我基本上将clearTimeout附加到全局变量:

//Function "countChar(val)" is activated with the keydown event
    var timer_to_op = null;
    function countChar(val) {
            var len = val.value.length; //lenght of input in textarea
            clearTimeout(timer_to_op);
    //...
    //irrelevant code
    //...
                      }else if(len > 140){
                        $("#status_toOp_num").text(145-len);
                        timer_to_op = setTimeout(function(){
                            val.value = val.value.substring(0, 140);
                        },5000);

    }

Actually, for some reason, it won't work. 实际上,由于某种原因,它将无法正常工作。 If the user is typing AND he types another char within the 5 seconds then I want the timer to restart. 如果用户正在键入并且他在5秒钟内键入了另一个字符,那么我希望计时器重新启动。

input: anotherwor  
input: anotherword (+d) timer = 1...2...3... 
input: anotherworde (+e) timer = 1...2...  
input: anotherwordef (+f) timer = 1...2...3...4...5!  
input: anotherwor     the user took more than 5 seconds so it erased the excedent.

Hope I got my point through. 希望我能理解我的意思。 Any ideas on this one? 关于这个有什么想法吗? Thank you very much! 非常感谢你! (I didn't put any html, it's only <textarea onclick="countChar(this)"> ) (我没有放任何html,只是<textarea onclick="countChar(this)">

I didn't really try to understand what you are doing in your code, seems unnecessarily complicated. 我并不是真的想了解您在代码中正在做什么,这似乎不必要地复杂。

Check this fiddle out. 检查这个小提琴。 http://jsfiddle.net/Q2h5f/ http://jsfiddle.net/Q2h5f/

HTML 的HTML

<textarea id="limitText">0123456789</textarea>

Javascript Java脚本

var tarea = document.getElementById("limitText");
tarea.onkeypress = checkInputText;
var timer = null;

function checkInputText(e) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(limitTextLength, 5000);
}

function limitTextLength() {
    var tareaVal = document.getElementById("limitText").value.toString();
    if (tareaVal.length > 10) {
        tarea.value = tareaVal.slice(0, 10);
    }
}

Should solve your issue. 应该解决您的问题。

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

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