以下是我正在处理的计时器脚本中的JavaScript setInterval
函数。
它基本上计算每秒的时间。我想要做的是使用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);
答案 0 :(得分:3)
您可以为此添加一个计数器来计算您的通话次数以及何时达到120,然后拨打电话让ajax发布数据。
var counter = 0 ;
timer.interval = setInterval(tick = function () {
/// your stuff
counter++;
if(counter == 120)
{
/// make ajax call
/// then
counter = 0;
}
}, 1000);