$(function() { $('#typing')....">

JavaScript counter

Each specific action is performed in JavaScript, for example:

<script type="text/javascript">
$(function() {
    $('#typing').keyup(function () {
     switch($(this).val()) {
       case 'containment':
// HERE
break;
     }
    });
});
</script>

inside // HERE will be where the counter was created. Then add 1 each time a “containment” case is executed, if it is not already running. So if I run the containment case once, it adds. If I run it again ... it will not - let's see what I'm talking about?

+3
source share
3 answers

You must determine the counter before starting the count. Then you can add to it later, for example:

<script type="text/javascript">
var count = 0;  //(or any number you need)
$(function() {
    $('#typing').keyup(function () {
     switch($(this).val()) {
       case 'containment':
          count++;
break;
     }
    });
});
</script>
+3
source

Like this:

<script type="text/javascript">
var shouldRunContainment = true;

$(function() {
    $('#typing').keyup(function () {
     switch($(this).val()) {
       case 'containment':
       if(shouldRunContainment) {
         shouldRunContainment = false;
         // Run containment
       }
       break;
     }
    });
});
</script>
+1
source

/ $('#typing').

:

var count = 0;
$('#typing').keyup(function () {
    switch($(this).val()) {
        case 'containment':
        // Will only run if the element does NOT have the containment class
        if(!$(this).hasClass('containment'){
            // Add containment class
            $(this).addClass('containment');
            // Counter
            count++;
        }
        break;
    }
});
+1

Source: https://habr.com/ru/post/1764009/


All Articles