function _query() { var _s = setInterval(function () { $.get('/...', function (data) { if (data == 1) clearInterval(_s); }); }, 1000); } 

-

 <a href="javascript://" onclick="_query()">test</a> 

That is, if you click on the test link 3 times, then 3 setInterval is called immediately, if 10, then 10, etc.

As you can when you click on the link at least 20-30 times, call only 1 setInterval . setTimeout no need to offer.

    1 answer 1

    I propose to introduce a sign of neglect function. If the flag is set, do not rerun the function. Something like this .


     var flag = false; // запрос не работает function _query() { if (!flag) { // если запрос не работает flag = true; // запрос работает _s = setInterval(function () { $.post('/echo/html/', { html: Math.round(Math.random() / 4 + 0.3) }, function (data) { $('#res').append(data + ", "); if (data == 1) { clearInterval(_s); flag = false; // и уже не работает } }); }, 1000); } } $('a').click(_query);​ 
    • @mantigatos, you are a genius! - ModaL
    • In this case, it is still possible to call $.get() precisely because of the author's inexplicable dislike of setTimeout. - KaZatsa