JavaScript睡姿不对
JavaScript does not sleep at the right position
我有个小问题。在我的主页上,我有一个按钮。这个Button通过onclick调用up函数(onclick="showhidelogin()").
函数如下所示:
function showhidelogin() {
document.getElementById("null").id = "menu-sticky";
sleep(1000);
document.getElementById("loginform").id = "loginformview";
}
为什么页面先等待,然后执行两个"getElementById"语句?
(setTimeout 也不起作用)
Javascript中没有sleep
函数。您必须使用 setTimeout
.
执行此操作
function showhidelogin() {
document.getElementById("null").id = "menu-sticky";
setTimeout(function () {
document.getElementById("loginform").id = "loginformview";
}, 1000);
}
改进 Ananth 的回答(检查 getElementById()
return 是否存在):
function showhidelogin() {
if(document.getElementById("null")) {
document.getElementById("null").id = "menu-sticky";
}
setTimeout(function () {
if (document.getElementById("loginform")) {
document.getElementById("loginform").id = "loginformview";
}
}, 1000);
}
我有个小问题。在我的主页上,我有一个按钮。这个Button通过onclick调用up函数(onclick="showhidelogin()").
函数如下所示:
function showhidelogin() {
document.getElementById("null").id = "menu-sticky";
sleep(1000);
document.getElementById("loginform").id = "loginformview";
}
为什么页面先等待,然后执行两个"getElementById"语句? (setTimeout 也不起作用)
Javascript中没有sleep
函数。您必须使用 setTimeout
.
function showhidelogin() {
document.getElementById("null").id = "menu-sticky";
setTimeout(function () {
document.getElementById("loginform").id = "loginformview";
}, 1000);
}
改进 Ananth 的回答(检查 getElementById()
return 是否存在):
function showhidelogin() {
if(document.getElementById("null")) {
document.getElementById("null").id = "menu-sticky";
}
setTimeout(function () {
if (document.getElementById("loginform")) {
document.getElementById("loginform").id = "loginformview";
}
}, 1000);
}