-3

So I want to add function to click to delay after clicking to stop spamming or macro usage. I normally use code like this,

<div onclick="return confirm('question?')>example </div>

but I want to it to delay the main function after clicking button. You can also suggest if you have any other suggestions to block spamming .

Berke
  • 81
  • 2
  • 12
  • thanks to those people who puts - to my question. instead of putting it maybe just go away and check other questions? – Berke Feb 02 '18 at 18:33
  • A delay doesn't really stop spamming. You should look into registering the timestamp of the clicks and compare it against current timestamp. – Adam Azad Feb 02 '18 at 18:33
  • How can I do that? Could you show me some small example. – Berke Feb 02 '18 at 18:34
  • 1
    If you want to prevent repeated firings in a short amount of time, you could use [debouncing](https://stackoverflow.com/questions/24004791/can-someone-explain-the-debounce-function-in-javascript) – noahnu Feb 02 '18 at 18:36

2 Answers2

2

If you use a setTimeout you wont be able to return the value of the confirm call, since it's done async. You will need to use a promise or a callback in order to use that value...

var time;

function do_confirm(question) {
  return new Promise(done => {
    if (time) clearTimeout(time);
    time = setTimeout(function() {
      done(confirm(question));
    }, 1000);
  })
}
<div onclick="do_confirm('yes or no?').then(resp=>alert('you said '+(resp?'yes':'no')));">click me a bunch </div>
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • is all code is written right by syntax? cuz it warns me about it. – Berke Feb 02 '18 at 18:50
  • @Berke - yeah, it's valid, but it might be warning you about the `>` in the arrow function.. feel free to use a standard function instead... it's only meant to demonstrate – I wrestled a bear once. Feb 02 '18 at 18:52
  • what is standart? pardon me. cuz I have really no info about js.. can you just show me the standart way. – Berke Feb 02 '18 at 18:53
  • we're really not meant to be free code writers here at SO, the idea is I give you the info you need and you apply it as you need it. if you don't understand how a function works I would suggest you read some tutorials. But it would be something like this.. `
    click me a bunch
    `
    – I wrestled a bear once. Feb 02 '18 at 18:55
1

It should be able to delay by using setTimeout(function(){ return confirm('question?'); }, <duration>);

Source: https://www.w3schools.com/jsref/met_win_settimeout.asp

However, like others have pointed out, this wouldn't be effective in deterring spamming.

  • instead of asking question? how I can make it just apply the code? without asking a question after 3 seconds. – Berke Feb 02 '18 at 18:36
  • I mean more like, when they press a button with macros like 10000 times. to stop this I need these js. – Berke Feb 02 '18 at 18:37
  • Like @noahnu pointed out, you should be able to use [debouncing](https://davidwalsh.name/javascript-debounce-function) to prevent repeated firing. – Brandon Nolet Feb 02 '18 at 18:38
  • Brandon, but we can also say that, this return confirm question may also block it yeah? – Berke Feb 02 '18 at 18:39
  • 1
    @Berke if a user is running a script to press a button 10000 times, then they can no longer expect a good user experience. They're using your software in a way that it's not intended to be used. There's no reason to handle that case. Even if you use debouncing like I mention in my comment, that's only intended for accidental repeated clicks. It's not a security measure, if that's what you're thinking. – noahnu Feb 02 '18 at 18:40
  • 1
    yeah you are right, maybe the best way is the use the question on click. – Berke Feb 02 '18 at 18:41