当在下面的代码中声明时,$sessionTimeLeft
在updateSession()
内被访问时未定义。但是当我在$sessionTimeLeft
中移动initiate()
声明和赋值时,我得到了正确的jQuery对象。我认为,由于关闭,我可以在$sessionTimeLeft
内声明initiate()
时访问$sessionTimeLeft
。但为什么var session = (function SessionManager() {
var timeLeftInMs;
var timeLeftInMin;
var delay; // in ms
var $sessionTimeLeft = $('#dcSessionTimeLeft');
/* Set up the module by setting the timeout and delay time. */
function initiate(_timeout, _delay) {
timeLeftInMin = _timeout;
timeLeftInMs = timeLeftInMin * 60 * 1000;
delay = _delay; // delay in ms
setInterval(updateSession, delay);
}
function updateSession() {
timeLeftInMs -= delay;
timeLeftInMin = timeLeftInMs / 60000; // convert ms to min.
$sessionTimeLeft.text(timeLeftInMin);
}
var publicAPI = {
initiate: initiate
};
return publicAPI;
})();
session.initiate(30,1000);
超出原始代码范围?
create table words
(
id int not null auto_increment primary key,
word varchar(255),
index(word)
);
答案 0 :(得分:1)
此处的问题是,当您尝试访问#dcSessionTimeLeft
时,可能尚未加载$(function() {
var session = (function SessionManager() {
var timeLeftInMs;
var timeLeftInMin;
var delay; // in ms
var $sessionTimeLeft = $('#dcSessionTimeLeft');
/* Set up the module by setting the timeout and delay time. */
function initiate(_timeout, _delay) {
timeLeftInMin = _timeout;
timeLeftInMs = timeLeftInMin * 60 * 1000;
delay = _delay; // delay in ms
setInterval(updateSession, delay);
}
function updateSession() {
timeLeftInMs -= delay;
timeLeftInMin = timeLeftInMs / 60000; // convert ms to min.
$sessionTimeLeft.text(timeLeftInMin);
}
var publicAPI = {
initiate: initiate
};
return publicAPI;
})();
session.initiate(30,1000);
});
。因此,确保加载它的一种方法是将代码包装在document.ready
:
<script type="text/javascript" src="js/jquery.js"></script>
<script>jQuery.noConflict();</script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/functions.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
checkConnection();
}
//------
function checkConnection() {
var networkState = navigator.connection.type;
if(networkState==Connection.NONE){
document.getElementById("main-content").innerHTML="<div style='text-align:center;'><img src='img/internet.png' style='max-width:100%;' /></div>";
}
}
//------
function teachers(){
var ref = cordova.InAppBrowser.open('http://ataksa.com/?p=serv4', '_self', 'location=no');
}
//------
function news(){
var ref = cordova.InAppBrowser.open('http://ataksa.com/?p=serv6', '_self', 'location=no');
}
//------
function jobs(){
var ref = cordova.InAppBrowser.open('http://ataksa.com/?p=serv7', '_self', 'location=no');
}
//------
function estb(){
var ref = cordova.InAppBrowser.open('http://ataksa.com/?p=serv8', '_self', 'location=no');
}
//------
function pro(){
var ref = cordova.InAppBrowser.open('http://ataksa.com/?p=serv9', '_self', 'location=no');
}
//------
function internal(){
var ref = cordova.InAppBrowser.open('http://ataksa.com/?p=serv10', '_self', 'location=no');
}
//------
function external(){
var ref = cordova.InAppBrowser.open('http://ataksa.com/?p=serv11', '_self', 'location=no');
}
//------
function online(){
var ref = cordova.InAppBrowser.open('http://ataksa.com/?p=serv12', '_self', 'location=no');
}
//------
</script>
答案 1 :(得分:0)
在这种情况下,封闭应该也能很好地工作。此代码无法正常工作的原因是您没有正确调用会话功能。请记住,此函数返回一个带有函数启动的对象
代码的最后一行应该是
session.initiate(30,1000);
这应该有效。为了重现我使用节点的问题。我将$ sessionTimeLeft变量更改为字符串hello并测试它是否仍可在updateSession函数中访问。我的代码是
var session = (function SessionManager() {
var timeLeftInMs;
var timeLeftInMin;
var delay; // in ms
var $sessionTimeLeft = "Hello";
/* Set up the module by setting the timeout and delay time. */
function initiate(_timeout, _delay) {
timeLeftInMin = _timeout;
timeLeftInMs = timeLeftInMin * 60 * 1000;
delay = _delay; // delay in ms
setInterval(updateSession, delay);
}
function updateSession() {
timeLeftInMs -= delay;
timeLeftInMin = timeLeftInMs / 60000; // convert ms to min.
console.log($sessionTimeLeft);
}
var publicAPI = {
initiate: initiate
};
return publicAPI;
})();
session.initiate(30,1000);
我得到的结果是
Hello
Hello
Hello
Hello
Hello
每1秒钟一次。