我想连接数据库并每隔1秒从一个字段中检索数据。这会加载第一条记录,但不会请求/显示任何新记录。有什么想法吗?
function test() {
var adOpenForwardOnly = 0,
adLockReadOnly = 1;
var conn = new ActiveXObject("ADODB.Connection");
conn.open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\path\\Data.mdb;Persist Security Info=False");
if (conn) {
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT Table1.* FROM Table1 where Time1 = time()", conn, adOpenForwardOnly, adLockReadOnly);
value1 = rs.fields("Value1").value;
document.write(value1);
}
rs.Close();
conn.Close();
}
setTimeout(test(), 1000);
答案 0 :(得分:0)
如果你这样尝试会怎么样?
function test() {
var adOpenForwardOnly = 0,
adLockReadOnly = 1;
try {
var conn = new ActiveXObject("ADODB.Connection");
conn.open("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\\path\\Data.mdb;Persist Security Info=False");
if (conn.State > 0) {
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT Table1.* FROM Table1 where Time1 = time()", conn, adOpenForwardOnly, adLockReadOnly);
value1 = rs.fields("Value1").value;
document.write(value1);
} else {
throw new Error('conn not open...');
}
rs.Close();
conn.Close();
} catch (someError) {
alert(someError.message);
}
}
var i = 0;
while (i === 0) {
setTimeout(test, 1000);
}
答案 1 :(得分:0)
您的代码存在两个问题:
setTimeout
代替setInterval
,因此它只会执行一次。()
将函数作为参数发送,但您应该只命名该函数,例如:setInterval(test, 1000);
另一个选项
我建议您使用递归setTimeout
(导致数据库延迟)。 E.g:
function test() {
var adOpenForwardOnly = 0,
adLockReadOnly = 1;
var conn = new ActiveXObject("ADODB.Connection");
conn.open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\path\\Data.mdb;Persist Security Info=False");
if (conn) {
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT Table1.* FROM Table1 where Time1 = time()", conn, adOpenForwardOnly, adLockReadOnly);
value1 = rs.fields("Value1").value;
document.write(value1);
}
rs.Close();
conn.Close();
setTimeout(test, 1000);
}
test();