-1
var input = document.getElementById("wordTyped");

input.onclick =  setInterval(countdown, 1000); 

function countdown() {
  console.log("test");
}

There are questions like this, but their problem is because they include "()" when calling the function. Mine don't and still the event runs automatically without the mouse clicking on the input element, did my searching and still can get any insight to this issue

Afiq Rosli
  • 357
  • 1
  • 7
  • 22
  • Read up on events ... https://www.w3schools.com/js/js_events.asp – anteAdamovic Nov 06 '17 at 12:55
  • @gurvinder372 The answer might be the same as the other post you marked as exact duplicate, but the situation is different, imo. Anyway, thank you for linking to the answer – Afiq Rosli Nov 06 '17 at 13:03
  • @AfiqRosli situation may be different but the issue you were facing *onclick event run automatically* is exactly due to the linked problem. You can try the linked solution and if there are more issues please raise a new question explaining the problem. – gurvinder372 Nov 06 '17 at 13:10
  • @gurvinder372 yes, you're right and nope no problem, fixed! I just didn't know what kind of keyword to get to the solution – Afiq Rosli Nov 06 '17 at 13:16

1 Answers1

0
input.onclick =  setInterval(countdown, 1000); 

In this case, the browser executes setInterval hoping to get a function as a return value. Of course, the return value is a number instead.

So, if you need to add a trigger the function at 1 second interval, after the click event, you should bind the click handler like this:

input.onclick =  function() {
    setInterval(countdown, 1000); 
};
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • "So, if you need to add a delay of one second" for this purpose, the method `setInterval()` ist the wrong choice. This method recalls the passed method. The better choice is `setTimeout()`. I can not write an answer any more because this question is closed. – Reporter Nov 06 '17 at 13:01
  • @reporter The OP intends to use `setInterval` - I had merely misread the method. – Nisarg Shah Nov 06 '17 at 13:24
  • @reporter I've checked `setTimeout ()`. From what I see, it does _something_ after the time ends. I'm trying to use `setInterval()` to change the html every second as it runs. If it's possible to manipulate the time argument in the `setTimeout ()`, might use for different purposes – Afiq Rosli Nov 06 '17 at 13:34
  • @AfiqRosli `setTimeout()' runs exactly one time, unless you have a recall within passed method. As the name 'setInterval()' says, it recalls the method automaticly without recall within passed method. You can replace the number by variable name in both methods. – Reporter Nov 06 '17 at 14:18