The concept is rather straightforward. The individual is monitoring the flash sale countdown every 10 milliseconds. As soon as the designated time is reached, they automatically locate and click the "Add to Cart" button on the webpage.
Allow me to elaborate further.
For instance:
A flash sale for Mi mobiles is scheduled to commence on October 17, 2016 at 2:00 PM. Utilizing JavaScript, this person is constantly verifying if the current time has aligned with the specified start time.
Normally, comparing dates directly can be tricky. To ensure accuracy, the date and time should be converted into milliseconds. This way, the timestamp for the flash sale date can be obtained.
var flashSaleTime = new Date("2016/10/17 02:00:00:000 PM").getTime();
Keep in mind: JavaScript adheres to the default date format of YYYY/MM/DD
, while the getTime()
method returns the date in milliseconds.
Hence, by checking whether the current time (in milliseconds) equals the flashSaleTime, we can dynamically trigger the Add to Cart button click.
var flashSaleTime = new Date("2016/10/17 02:00:00:000 PM").getTime();
setInterval(function(){
var currentTime = Math.floor((new Date).getTime()/1000);
if(currentTime*1000 === flashSaleTime){
document.getElementsByTagName('a')[43].click();
}
},10);
In this scenario, the setInterval function evaluates the condition every 10 milliseconds. Once the present time matches the target time, the program locates the button reference and initiates a click event on it.