[JavaScript] I want to limit the processing by checking the input items when the specified time elapses at the time of focus out.

[What I want to ask]

But it was implemented check processing in the input item after a specified time and when the focus is out, and exit to the JavaScript function, view and has an error message twice display in the alert will be called both for processing there is no Will be done.

[What I want to achieve]

I want to issue an alert only once. I want to check every time I get out of focus. I want to check even after the specified time has passed.

[Implemented content, what you are thinking]

main.js


function isTimecheck() {
   setTimeout("onAutoSave()",Specified time);
}

function onAutoSave() {
    var result = true;
    var a =Input items.value; //Get the input value of the input item.
    var maxLen = 20; //Maximum number of characters: 20 in the example.
    if (a != '') {
        result = WordCountCheck(a, maxlen);
        if (result == false) {
            alert(Error message);
Input items.focus(); //Return the focus to the input item.
            return false;
        }
    }
}
window.onload = function(){
    isTimeCheck();
}

check.js


function WordCountCheck(str, maxLength) {
    var i = 0; 
    var len = 0; //Store the number of bytes.
    str.value = rightTrim(str.value);//Delete unnecessary right space.
    var val = str.value;
    if(val !=""){
        for(; i<val.length; i++) {
If it is half-width, it is 1 byte, so add 1 to len.
If it is full-width, it is 2 bytes, so add 2 to len.
        }
        if(maxLength < len){
            alert(Error message);
            str.focus(); //Return the focus to the input item.
            return false;
        }
    }
}

Test.html


<TD>
    <textarea cols="" rows="" name="" style="" onBlur="WordCountCheck(this, 20);">Input items</textarea>
</TD>

This HTML part

<TD>
    <textarea cols="" rows="" name="" style="" onChange="WordCountCheck(this, 20);">Input items</textarea>
</TD>

In this case, when I tried with the same data as onBlur, the pop-up appeared only once, but if this is the case, the value changes and an error message appears and then the focus is out without changing. I'm dissatisfied because I can't check it.

Also, the check process is described in another js file.

As a response, I am now wondering if the following is feasible. (1) Check only onBlur, and if there is an error, exit the process at the beginning of onAutoSave. (2) Prevent the onBlur process from being called when the specified time has elapsed. ③ If a check error occurs in the one called first by onBlur or onAutoSave, the process is exited at that point (I think this is possible if the process of exit can be realized ...).

In the first place, in the case of this implementation, which of onBlur and onAutoSave will be executed first after the specified time has elapsed?

[What I tried]

When I tried to make the onBlur part as follows (listed in JS for easy viewing), of course, it is not checked at the time of lost focus, and it is not checked even after the specified time has passed. It seems that onAutoSave was called earlier, so if a check error occurs there, I tried to set the global variable checkFlg to true so that onBlur processing is not performed. However, the alert appears twice.

onBlur="isLostFocusCheck(this);"
var checkFlg=false;
function isLostFocusCheck(str) {
    if(checkFlg==false) { //If there is a check error in auto save, checkFlg is true and this if should be passed through.
        WordCountCheck(str, 20);
    }
}

Recommended Posts

[JavaScript] I want to limit the processing by checking the input items when the specified time elapses at the time of focus out.
I want to limit the input by narrowing the range of numbers
I want to find out if the specified character string is supported by the target character code
I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
I want to understand the flow of Spring processing request parameters
I want to make the frame of the text box red when there is an input error
A memo when you want to clear the time part of the calendar
I want to know the JSP of the open portlet when developing Liferay
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
[RxSwift] I want to deepen my understanding by following the definition of Observable
I want to output the day of the week
I want to var_dump the contents of the intent
I tried to create a log reproduction script at the time of apt install
[JavaScript] I can't get the response body at the time of error with axios (ajax)
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to find out which version of java the jar file I have is available
[Java] I want to write asynchronous processing using Promise in Java-Trial of Promise-like grammar of JavaScript-
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
When you want to change the MySQL password of docker-compose
I want to narrow down the display of docker ps
I want to return multiple return values for the input argument
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController