JavaScript setInterval 使 AJAX post 每 X 分钟?
JavaScript setInterval to make AJAX post every X number of minutes?
下面是我正在处理的计时器脚本中的 JavaScript setInterval
函数。
它基本上是每秒计算一个时间。我想要做的是 post 这次每 2 分钟向服务器发送一次 AJAX 请求。
我只需要帮助确定何时达到每 2 分钟,以便我可以插入我的 AJAX 代码。
谁能告诉我如何在下面的代码中确定每 2 分钟的间隔?
timer.interval = setInterval(tick = function () {
/* Don't do anything if the timer is paused */
if (timer.paused) return;
/* Calculate the number of seconds from the startTime */
var sec = M.max(M.round((Date.now() - startTime) * dir / 1000), 0);
var val = {
S: sec, /* Total number of seconds */
s: sec % 60, /* Seconds */
M: M.floor(sec /= 60), /* Total minutes */
H: M.floor(sec /= 60), /* Total hours */
D: M.floor(sec /= 24) /* Total days */
};
val.m = val.M % 60; /* Minutes */
val.h = val.H % 24; /* Hours */
val.d = val.D; /* Days */
/* Format the timer */
val.text = (options.format || '%-H{:}%0m:%0s').replace(
/%(-?)(0?)([dhms])(\s*)(?:\{(.+?)\})?/ig,
options.replacer || function (match, omit, zero, part, space, forms) {
/* The value of the selected part */
var v = val[part];
/*
* 'day' -> [ 'day', 'day', 'day' ]
* 'day|days' -> [ 'day', 'days', 'days' ]
*/
(forms = (forms||'').split('|'))[2] =
forms[2] || (forms[1] = forms[1] || forms[0]);
/*
* Return the output text, or an empty string if the value is
* zero and isn't supposed to be shown
*/
return !v && omit ? '' :
/*
* Initialize the output text with the value (and optionally
* a leading zero)
*/
(v > 9 ? '' : zero) + v + space +
/* Add the appropriate form */
forms[+(v != 1) +
(v != 1 && (v%10 < 2 || v%10 > 4) ||
(v > 10 && v < 20))];
});
/*
* If we have an element, put the formatted text inside it
* (otherwise, set "timerElem" to this instance of tinyTimer, so that it gets
* passed to callbacks)
*/
timerElem ? $(timerElem).html(val.text) : timerElem = timer;
/* Invoke the onTick callback (if defined) */
(options.onTick || doNothing).call(timerElem, timer.val = val);
}, 1000);
您可以为此添加一个 计数器 来计算您的呼叫,当它达到 120 时,然后调用 ajax 到 post 数据.
var counter = 0 ;
timer.interval = setInterval(tick = function () {
/// your stuff
counter++;
if(counter == 120)
{
/// make ajax call
/// then
counter = 0;
}
}, 1000);
下面是我正在处理的计时器脚本中的 JavaScript setInterval
函数。
它基本上是每秒计算一个时间。我想要做的是 post 这次每 2 分钟向服务器发送一次 AJAX 请求。
我只需要帮助确定何时达到每 2 分钟,以便我可以插入我的 AJAX 代码。
谁能告诉我如何在下面的代码中确定每 2 分钟的间隔?
timer.interval = setInterval(tick = function () {
/* Don't do anything if the timer is paused */
if (timer.paused) return;
/* Calculate the number of seconds from the startTime */
var sec = M.max(M.round((Date.now() - startTime) * dir / 1000), 0);
var val = {
S: sec, /* Total number of seconds */
s: sec % 60, /* Seconds */
M: M.floor(sec /= 60), /* Total minutes */
H: M.floor(sec /= 60), /* Total hours */
D: M.floor(sec /= 24) /* Total days */
};
val.m = val.M % 60; /* Minutes */
val.h = val.H % 24; /* Hours */
val.d = val.D; /* Days */
/* Format the timer */
val.text = (options.format || '%-H{:}%0m:%0s').replace(
/%(-?)(0?)([dhms])(\s*)(?:\{(.+?)\})?/ig,
options.replacer || function (match, omit, zero, part, space, forms) {
/* The value of the selected part */
var v = val[part];
/*
* 'day' -> [ 'day', 'day', 'day' ]
* 'day|days' -> [ 'day', 'days', 'days' ]
*/
(forms = (forms||'').split('|'))[2] =
forms[2] || (forms[1] = forms[1] || forms[0]);
/*
* Return the output text, or an empty string if the value is
* zero and isn't supposed to be shown
*/
return !v && omit ? '' :
/*
* Initialize the output text with the value (and optionally
* a leading zero)
*/
(v > 9 ? '' : zero) + v + space +
/* Add the appropriate form */
forms[+(v != 1) +
(v != 1 && (v%10 < 2 || v%10 > 4) ||
(v > 10 && v < 20))];
});
/*
* If we have an element, put the formatted text inside it
* (otherwise, set "timerElem" to this instance of tinyTimer, so that it gets
* passed to callbacks)
*/
timerElem ? $(timerElem).html(val.text) : timerElem = timer;
/* Invoke the onTick callback (if defined) */
(options.onTick || doNothing).call(timerElem, timer.val = val);
}, 1000);
您可以为此添加一个 计数器 来计算您的呼叫,当它达到 120 时,然后调用 ajax 到 post 数据.
var counter = 0 ;
timer.interval = setInterval(tick = function () {
/// your stuff
counter++;
if(counter == 120)
{
/// make ajax call
/// then
counter = 0;
}
}, 1000);