Light12306/Web12306/js/account/keepalive.js
2014-07-29 21:19:06 +08:00

46 lines
957 B
JavaScript

define(function (require, exports, module) {
var ajax = require("../platform/webRequest.js");
var interval = 1 * 60 * 1000;
var timer = null;
var started = false;
var checkUser = function () {
timer = null;
ajax.sendPost("login/checkUser", "login/init", null, "json", function () {
var m = this.model;
if (!m || !m.status) {
if (started)
timer = setTimeout(checkUser, interval);
}
else if (!m.data || !m.data.flag) {
//登录无效
document.dispatchEvent(new CustomEvent("loginInvalid"));
}
else {
ajax.userAtts = m.data.attributes;
if (started)
timer = setTimeout(checkUser, interval);
}
}, function () {
if (started)
timer = setTimeout(checkUser, interval);
});
};
//
exports.start = function () {
started = true;
if (!timer)
checkUser();
return true;
};
exports.stop = function () {
started = false;
if (timer)
clearTimeout(timer);
return true;
};
});