-3

What do I need to add to make the button turn off if pressed 10 times in less than 1 minute but keep counting if not pressed in 1 minute?

function buttonClicked() {
  if (totalCount + accCounter > 9) {
    document.getElementById("btn").disabled = true;
  }
}


document.getElementById('btn').onclick = function() {
  upClickCounter();
  buttonClicked()
}

function upClickCounter() {
  const clickCounter = document.getElementById("clicker");
  const totalClickCounter = document.getElementById('totalCounter');

  accCounter++;
  clickCounter.children[0].innerText = '+' + accCounter;
  totalClickCounter.innerText = totalCount + accCounter;

}

let accCounter = 0;
let totalCount = 0;
<button id="btn">Click me</button>

<div id="clicker">
  <span></span>
</div>
<div id="totalCounter"></div>
CIAO
  • 1
  • 3
  • 2
    It sounds like you need a [`setTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout) call, but the description of what you're trying to achieve isn't really clear. – Rory McCrossan Jan 21 '21 at 15:20
  • Agreed, the `buttonClicked` function is what throws me off. – JakeAve Jan 21 '21 at 15:25
  • The description doesn't describe a setTimeout to me - that would disable the button after a minute so it couldn't be clicked. The line *"keep counting it not pressed in 1 minute"* could indicate that any clicks after 1 minute would be ignored. So depends on what you actually want - block clicks or ignore clicks (after 1 min) – freedomn-m Jan 21 '21 at 15:57
  • On load (or "start") set a variable to the start time, when it's clicked, check if current time > 1 minute from the start time. – freedomn-m Jan 21 '21 at 15:58
  • Thanks all of you i wanted a button that turn off if pressed 10 times in less than 1 minute but keep counting (if not pressed 10 times in 1 minute – CIAO Jan 21 '21 at 16:02
  • So, within *any* 1 minute period? eg I click it once, wait 5 mins then click 10 times really quickly and it gets disabled? Or I click once, wait 35 seconds, click again, wait 35 seconds (so first has timed out) then need to click 9 more times within a minute of the 2nd click)? – freedomn-m Jan 21 '21 at 16:08
  • keep counting if not clicked under minute in the sense? – nTheta Jan 21 '21 at 16:18
  • you can click nine times in a minute but nothing happens but the tenth time the button turns off, but if the minute has passed you can keep clicking but always for a maximum ten times and the counter does not reset but continues to increase in number. – CIAO Jan 21 '21 at 16:21
  • Something like: https://jsfiddle.net/tda1cmbv/ ? (added some variables so you can test, currently set to 5 times in 5 seconds so you don't have to wait a whole minute to test it) – freedomn-m Jan 21 '21 at 16:24
  • freedomn-m thank you that's just what I was looking for but how can I implement it in the script that I script I posted above using only javascript is it possible. – CIAO Jan 21 '21 at 17:03
  • Don't tag jquery if you don't want jquery... you can try converting with http://youmightnotneedjquery.com/ - it's only the click event handler and disable code you need to convert the rest is already js or just showing what's happening – freedomn-m Jan 22 '21 at 00:26

2 Answers2

0

setTimeout(countOver, 10000);  //set for 10 seconds

function countOver(){
  document.getElementById('btn').disabled=true
}



function buttonClicked() {
  if (totalCount + accCounter > 9) {
    document.getElementById("btn").disabled = true;
  }
}


document.getElementById('btn').onclick = function() {
  upClickCounter();
}

function upClickCounter() {
  const clickCounter = document.getElementById("clicker");
  const totalClickCounter = document.getElementById('totalCounter');

  accCounter++;
  clickCounter.children[0].innerText = '+' + accCounter;
  totalClickCounter.innerText = totalCount + accCounter;

}

let accCounter = 0;
let totalCount = 0;
<button id="btn">Click me</button>

<div id="clicker">
  <span></span>
</div>
<div id="totalCounter"></div>
DCR
  • 14,737
  • 12
  • 52
  • 115
0

You need a setTimeout. It takes in a callback function and a timer in milliseconds. I updated upClickCounter with a line that disables the button, then a setTimeout that will re-enable the button after 1 minute (60,000 milliseconds).

EDIT:

I think I understand what you want, and can only wonder why, but this should do what you are describing.

let accCounter = 0;
let totalCount = 0;
let lessThanOneMinute = true;

function upClickCounter(event) {
    const clickCounter = document.getElementById("clicker");
    const totalClickCounter = document.getElementById('totalCounter');
    accCounter++;
    clickCounter.children[0].innerText = '+' + accCounter;
    totalClickCounter.innerText = totalCount + accCounter;

    // If the click count is greater or equal to 10 and the lessThanOneMinute is still true, disable the button
    if (accCounter >= 10 && lessThanOneMinute) { event.target.disabled = true; }
}

document.getElementById('btn').onclick = upClickCounter;

setTimeout(() => { lessThanOneMinute = false }, 60000); // the lessThanOneMinute variable will become false in one minute
JakeAve
  • 279
  • 2
  • 6
  • Thank you but i wanted a button that turn off if pressed 10 times in less than 1 minute but keep counting (if not pressed 10 times in 1 minute)? – CIAO Jan 21 '21 at 16:00
  • I updated the answer. I hope that points you on the right track. – JakeAve Jan 21 '21 at 16:28