【问题标题】:Specify the start time of the command according to the website clock根据网站时钟指定命令的开始时间
【发布时间】:2020-07-21 15:16:56
【问题描述】:

我有这个自动点击按钮代码,我想根据特定时间的网络时钟为其设置(添加)开始时间。 非常感谢您的编辑并完成它。

这是我的代码:

let mainInterval =setInterval(() => {
    
 document.querySelector('.buy button').click();

    console.log('clicked');
}, 400);

 
let stop = 4000;     
setTimeout(() => { clearInterval(mainInterval); alert('stop'); }, stop);

网站时钟元素:

class="show-clock" clock="" now-time="nowTime">20:20:20

【问题讨论】:

  • 您只想在特定时间执行某事?你可以计算现在和这个特定时间之间的差异,并将这个偏移量放入setTimeout ?
  • 是的,兄弟,我想在时钟时间 8:00:00 执行这段代码...我想将时间直接放入代码中
  • setTimeout(() => ..., new Date(your8DateTime) - new Date())

标签: javascript html button time click


【解决方案1】:

解决办法:

const scheduleTrigger = (futureDate) => {
    const timeMS = new Date(futureDate) - new Date();

    return new Promise((res, ref) => {
        if (timeMS > 0) {
            setTimeout(() => {
                res();
            }, timeMS);
        } else {
            rej();
        }
    })
}

//const futureDate = '2020-07-23T20:53:12';
// or
const futureDate = new Date();
futureDate.setSeconds(futureDate.getSeconds() + 5);

console.log('now');

scheduleTrigger(futureDate).then(_ => {
    console.log('future date reached');
    // start whatever you want
}).catch(_ => {
    // the date provided was in the past
});

【讨论】:

  • 谢谢兄弟。我希望我的代码在这个时候(8:00:00)开始。你能完成我的代码吗?
猜你喜欢
  • 2019-06-22
  • 2022-01-07
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 2013-06-24
相关资源
最近更新 更多