JavaScript
var SM = SM || {};
SM.keepAlive = function () {
var aliveUrl = "https://blog.maskalik.com/keep-me-alive.aspx",
interval = 60 * 1000 * 10,
that = {},
alive = false,
timer = {};
that.isActive = function () {
alive = true;
};
that.removeTrackingIframeIfPresent = function () {
var frame = document.getElementById("keep-alive-iframe");
if (frame) {
document.body.removeChild(frame);
}
};
that.sendKeepAlive = function () {
if (alive) {
that.removeTrackingIframeIfPresent();
var frame = document.createElement("iframe");
frame.setAttribute("src", aliveUrl);
frame.setAttribute("id", "keep-alive-iframe");
frame.setAttribute("style", "display:none");
document.body.appendChild(frame);
alive = false;
}
};
that.start = function () {
var allTextareas = $("textarea");
allTextareas.live("keyup", that.isActive);
var allLinks = $("a");
allLinks.live("click mouseover", that.isActive);
timer = setInterval(that.sendKeepAlive, interval);
};
return that;
};
调用方式
JavaScript
var alive = SM.keepAlive();
alive.start();
引用:https://blog.maskalik.com/javascript/cross-domain-keep-me-alive/