I
I
IvanSkor2020-09-28 09:43:04
JavaScript
IvanSkor, 2020-09-28 09:43:04

How to add milliseconds to the script on click at the right time?

hello, I have this script:
window.setInterval(function(){ // Check interval
var date = new Date();
if(date.getHours() === 14 && date.getMinutes() === 00 && date. getSeconds() === 0){
document.querySelector('button.button-submit').click() // <-- here
}
}, 1000); // Repeat check every second
how to add millisecond conditions? I will be grateful for the answer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Absolute138, 2020-09-28
@IvanSkor

You won't get that much accuracy.
+-1 ms...
The script will switch to execution once every 1ms when the difference between the current time and the execution time is less than the starting interval .
I sketched it on my knee ... and there is a lot to optimize here))

function run(interval){
  var timer = setInterval(function(){ 
     var d = new Date(),
         unixMidNight = Date.parse([d.getMonth()+1,d.getDate(),d.getFullYear()].join('/'));
     var unix = unixMidNight + 14*60*60*1000; //14:00 текущего дня
     var current = new Date().getTime();
     if(interval != 1 && unix - current <= interval){
        clearInterval(timer);
        run(1);
     }
     if(unix <= current){
        document.querySelector('button.button-submit').click();
        clearInterval(timer);
      }
  }, interval);   
}
run(5000);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question