JavaScript Calling a function only one time while submiting javascript calling function only once

I am trying to submit a form and while submitting I call a function which is myOwnFun and it runs. But if I keep pressing enter for long time, form submit repeats which I don't want.

Here is the code..

<form class="user__answer-form">
     <input type="text" name="useranswer" class="user__answer">
</form> 
const form = document.querySelector('.user__answer-form');
const formAnswer = document.querySelector('.user__answer');

formAnswer.focus();

form.addEventListener('submit', function(e) {
   e.preventDefault();
   myOwnFun();
});


function myOwnFun() {
   console.log('show text one time in one Enter')
}

I want to run this function only one time when I press enter even if I pressed for longer time. And I need to submit the form many different times.

Answer:1

const form = document.querySelector('.user__answer-form');
const formAnswer = document.querySelector('.user__answer');

//create a counter to track key down events
let count  = 0;

formAnswer.focus();

form.addEventListener('submit', function(e) {
   e.preventDefault(); 
   //call your function only if count is 0 
   //ie is first time enter key is pressed 
   
   //if key is pressed for  a long time count > 0 
   // this condition fails
   if(count == 1){
     myOwnFun();
   }    
    
});

//fires on any key is pressed on form element
form.addEventListener('keydown', function(e) {
    // for cross browser compatibility
    const keyCode = e.which || e.keyCode;

   if(keyCode == 13){
      //if enter key is pressed update count 
      count ++;
   }
});

//fires on  key is released on form element
form.addEventListener('keyup', function(e) {

      const keyCode = e.which || e.keyCode;
      //if enter key is released reset the count
      if(keyCode == 13){
        count = 0
      }

});



function myOwnFun() {
    console.log('show text one time in one Enter');
}
<form class="user__answer-form" >
     <input type="text" name="useranswer" class="user__answer">
</form>
Answer:2

Try this:

const form = document.querySelector('.user__answer-form');
const formAnswer = document.querySelector('.user__answer');

formAnswer.focus();

form.addEventListener('submit', function(e) {
   e.preventDefault();
   myOwnFun();
});


function myOwnFun() {
 var executed = false;
    return function() {
        if (!executed) {
            executed = true;
            console.log('show text one time in one Enter')
        }
    };

}
Answer:3

you can use that: form.removeEventListener("submit", yourFunction);

I think you'll have to change function(e) to a "real function" to make it work

Answer:4

Use e.preventDefault() method in your script first and then in your code, this will disable the default functionality of form and executing your code.

Answer:5

Maybe I am being stupid here but I cannot figure out how to respond to a Foundation 5 TopBar dropdown selection with a javascript function. I would like to select a theme for a library I use. To set ...

Maybe I am being stupid here but I cannot figure out how to respond to a Foundation 5 TopBar dropdown selection with a javascript function. I would like to select a theme for a library I use. To set ...

I created a class which sets up a pausable rxjs observable on an interval: export class RepeatingServiceCall<T> { private paused = false; private observable: Observable<T>; ...

I created a class which sets up a pausable rxjs observable on an interval: export class RepeatingServiceCall<T> { private paused = false; private observable: Observable<T>; ...

  1. restart timer rxjs

I'm trying to scrap some information from this ASP page http://laredoute.fr/ppdp/prod-350007615.aspx mainly the first 4 images in high resolution that load in the image-carousel. Depending on the ...

I'm trying to scrap some information from this ASP page http://laredoute.fr/ppdp/prod-350007615.aspx mainly the first 4 images in high resolution that load in the image-carousel. Depending on the ...

  1. scrapy javascript pagination
  2. scrapy javascript support
  3. scrapy javascript click
  4. scrapy javascript onclick
  5. scrapy javascript site
  6. scrapy javascript generated content
  7. scrapy javascript rendered pages
  8. scrapy javascript link
  9. scrapy javascript next page
  10. scrapy javascript python
  11. scrapy javascript example
  12. scrapy javascript selenium
  13. scrapy javascript website
  14. scrapy javascript variable
  15. scrapy javascript button
  16. scrapy javascript form
  17. scrapy javascript table
  18. scrapy javascript postback
  19. scrapy javascript ajax
  20. scrapy javascript text

I was working on a small JavaScript project to make a tile based game (not homework, don't worry!), however I ran into a problem with my 'tile' class. Each 'tile' has an X and a Y property, for its X ...

I was working on a small JavaScript project to make a tile based game (not homework, don't worry!), however I ran into a problem with my 'tile' class. Each 'tile' has an X and a Y property, for its X ...