1

I have a JS function. I want it to be paused after it's first-time execution for 10 s, and after that time I want to resume it.

So, what I have done is-

$( "#button_yes,#button_no" ).click(function()
{
    if(counter)
    {
        console.log('Done');
        $.ajax(
        {
            url: baseURL+"add_votes",
            success: function(result)
            {
                counter=0;
                alert(result);
                setTimeout(check, 10000); //set a 10s  delay
                counter=1;
            },
            method: "POST",
            data:
                {
                    id : '1'
                },
        });
    }
});

But it I not working perfectly. It prevents the function executing second time permanently if I click multiple times.

Is there any solution?

Thanks for helping

Abrar Jahin
  • 13,156
  • 22
  • 105
  • 151

1 Answers1

3

How about keeping track of the last time the button was clicked and ensuring that 10s has elapsed before processing the next click?

var lastClicked = 0;
$( "#button_yes,#button_no" ).click(function()
{
    var now = new Date();
    if(now - lastClicked > 10000)
    {
        lastClicked = now;
        console.log('Done');
        $.ajax(
        {
            url: baseURL+"add_votes",
            success: function(result)
            {
                alert(result);
            },
            method: "POST",
            data:
                {
                    id : '1'
                },
        });
    }
});
arcyqwerty
  • 9,523
  • 3
  • 43
  • 81