-1
以下是我正在處理的計時器腳本中的一個JavaScript setInterval
函數。JavaScript setInterval每隔X幾分鐘發佈一次AJAX?
它基本上每秒計算一次。我想要做的就是每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);
您可以使用** Counter **。意味着計數呼叫,當它達到120時,則進行ajax呼叫。 –
你不能使用'setInterval(function(){//傳遞timer.interval通過ajax},120000)'? –
@MuhammadUsman我看到了,所以增加計數器,當它達到120使我的AJAX調用和重置計數器回到0,所以它將啓動過程 – JasonDavis