Javascript 每 "X" 分钟调用一次函数
Javascript call function at each "X" minutes by clock
好的,所以我有:
function show_popup() {
alert('Ha');
}
现在,我想要的是在每 X 分钟调用此函数 BUT 给出时钟(实时)作为参考.
如果X为5,则下一个函数正常运行:
setInterval(function(){
var date = new Date();
var minutes = date.getMinutes().toString();
var minutes = minutes.slice(-1); // Get last number
if(minutes == 0 || minutes == 5)
{
show_popup(); // This will show the popup at 00:00, 00:05, 00:10 and so on
}
}, 1000);
如果我将 5 分钟更改为 4 分钟、3 分钟或 20 分钟,如何使此功能起作用?
我必须提一下,我无法通过 setinterval 更改计时器,因为这意味着只有当您在 X 分钟后进入页面时才会触发弹出窗口。我不想要那个。我想在特定分钟显示弹出窗口,为时钟提供参考。
您需要找到 X
的 multiples
为此,您可以使用 modulo operation,因此:
if(minutes % X === 0) {
show_popup();
}
模运算将 return 除法的其余部分 在 a 和 b[=31 之间=],如果那是 0,那就意味着 b 是 a.
的 multiple
例如,如果你想每 3 分钟显示一次:
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0 //show
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0 //show
等等...
两种方式,只需运行代码即可查看结果(在chrome浏览器中)
1.use 计时器,您可以在下一次滴答到来时更改周期,计时器不是那么精确
class MyInterval {
constructor(defaultInterval, callback) {
this.interval = defaultInterval
this.callback = callback
this._timeout = null
this.tick()
}
tick() {
const {
interval,
callback
} = this
this._timeout = setTimeout(() => {
callback()
this.tick()
}, interval)
}
stop() {
clearTimeout(this._timeout)
}
changeInterval(interval) {
this.interval = interval
}
}
const myInterval = new MyInterval(1000, () => console.log(new Date()))
setTimeout(() => {
myInterval.changeInterval(2000)
}, 3500)
setTimeout(() => {
myInterval.stop(2000)
}, 13500)
2.use最小间隔,反应更快,有最小限度,可能花费更多
class MyInterval {
constructor(minimal, defaultInterval, callback) {
this.minimal = minimal
this.interval = defaultInterval
this.callback = callback
this._current = 0
this._timeout = setInterval(() => {
this._current++
if (this._current >= this.interval) {
this._current = 0
callback()
}
}, minimal)
}
stop() {
clearInterval(this._timeout)
}
changeInterval(interval) {
this.interval = interval
}
}
const myInterval = new MyInterval(1000, 1, () => console.log(new Date()))
setTimeout(() => {
myInterval.changeInterval(2)
}, 3500)
setTimeout(() => {
myInterval.stop()
}, 13500)
好的,所以我有:
function show_popup() {
alert('Ha');
}
现在,我想要的是在每 X 分钟调用此函数 BUT 给出时钟(实时)作为参考.
如果X为5,则下一个函数正常运行:
setInterval(function(){
var date = new Date();
var minutes = date.getMinutes().toString();
var minutes = minutes.slice(-1); // Get last number
if(minutes == 0 || minutes == 5)
{
show_popup(); // This will show the popup at 00:00, 00:05, 00:10 and so on
}
}, 1000);
如果我将 5 分钟更改为 4 分钟、3 分钟或 20 分钟,如何使此功能起作用?
我必须提一下,我无法通过 setinterval 更改计时器,因为这意味着只有当您在 X 分钟后进入页面时才会触发弹出窗口。我不想要那个。我想在特定分钟显示弹出窗口,为时钟提供参考。
您需要找到 X
的 multiples为此,您可以使用 modulo operation,因此:
if(minutes % X === 0) {
show_popup();
}
模运算将 return 除法的其余部分 在 a 和 b[=31 之间=],如果那是 0,那就意味着 b 是 a.
的 multiple例如,如果你想每 3 分钟显示一次:
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0 //show
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0 //show
等等...
两种方式,只需运行代码即可查看结果(在chrome浏览器中)
1.use 计时器,您可以在下一次滴答到来时更改周期,计时器不是那么精确
class MyInterval {
constructor(defaultInterval, callback) {
this.interval = defaultInterval
this.callback = callback
this._timeout = null
this.tick()
}
tick() {
const {
interval,
callback
} = this
this._timeout = setTimeout(() => {
callback()
this.tick()
}, interval)
}
stop() {
clearTimeout(this._timeout)
}
changeInterval(interval) {
this.interval = interval
}
}
const myInterval = new MyInterval(1000, () => console.log(new Date()))
setTimeout(() => {
myInterval.changeInterval(2000)
}, 3500)
setTimeout(() => {
myInterval.stop(2000)
}, 13500)
2.use最小间隔,反应更快,有最小限度,可能花费更多
class MyInterval {
constructor(minimal, defaultInterval, callback) {
this.minimal = minimal
this.interval = defaultInterval
this.callback = callback
this._current = 0
this._timeout = setInterval(() => {
this._current++
if (this._current >= this.interval) {
this._current = 0
callback()
}
}, minimal)
}
stop() {
clearInterval(this._timeout)
}
changeInterval(interval) {
this.interval = interval
}
}
const myInterval = new MyInterval(1000, 1, () => console.log(new Date()))
setTimeout(() => {
myInterval.changeInterval(2)
}, 3500)
setTimeout(() => {
myInterval.stop()
}, 13500)