Light12306/Web12306/js/account/keepalive.js

46 lines
957 B
JavaScript
Raw Normal View History

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