Good day. There is a button to switch the page loading via the Jsoup parser. If the button is pressed many times, a queue is formed and Jsoup as each download yields each result of a click. Required: if the button is pressed frequently (for example, more often 1p in 1 second), then the event does not work. If it is pressed and 1 second has passed, the event is triggered. Please tell me how this can be implemented. Thank.
3 answers
An example of the implementation of pressing the "back" button.
In the class variable, declare private long back_pressed;
Then, create a click capture:
@Override public void onBackPressed() { if (back_pressed + 2000 > System.currentTimeMillis()) { stopAllServices(); finish(); } else { Toast.makeText(MainActivity.this, "Чтобы выйти, нажмите на кнопку НАЗАД ещё раз.", Toast.LENGTH_LONG).show(); back_pressed = System.currentTimeMillis(); } }
Hope this helps you. Good luck!
|
- get a
long
variable in which thetimeStamp
last click will be stored. - When pressed, compare the current time with the value of this variable.
- If the difference is greater than 1000ms, then complete the request, otherwise do nothing.
- Immediately after calculating the time difference, update the value of the variable from item 1 with the current time
|
for example:
long prevTime = 0; private void sumbmit() { long currentTime = System.currentTimeMillis(); boolean isAction = false; synchronized (this) { if (currentTime - prevTime > TimeUnit.SECONDS.toMillis(10)) { prevTime = currentTime; isAction = true; } } if (isAction) { //что то там } }
- Chet does not want to work.
TimeUnit.SECONDS.toMillis(10)
, 10 is it in ms? - Pollux - 10 seconds in milliseconds. - Artem Konovalov
|