怎样在异步的情况下正常执行?

「已注销」 2016-09-27 09:31:33
现在用的是false(同步),最后的document是可以正常替换的,但是异步下就不行,请问要怎么修改?


var sawnow = new Date();
var sawmonth = sawnow.getMonth() + 1;
var sawdate = sawnow.getDate();
var sawbg = "$_G['style'][styleimgdir]/Moe/MoeSaw/bg/" + sawmonth + "-" + sawdate + ".jpg";
var sawbgs = "url($_G['style'][styleimgdir]/Moe/MoeSaw/bg/" + sawmonth + "-" + sawdate + ".jpg)";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", sawbg, false);
xmlhttp.send();
if(xmlhttp.readyState===4) {
if(xmlhttp.status===200) {
document.getElementById("moesaw").style.background = sawbgs;
}
}
...全文
47 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2016-09-27
  • 打赏
  • 举报
回复
引用 1 楼 functionsub 的回复:
xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState===4) { 
            if(xmlhttp.status===200) {
                document.getElementById("moesaw").style.background = sawbgs;
            }
        }
}
非常感谢,问题解决了,异步操作我还得多向大家学习呢
functionsub 2016-09-27
  • 打赏
  • 举报
回复
xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState===4) { 
            if(xmlhttp.status===200) {
                document.getElementById("moesaw").style.background = sawbgs;
            }
        }
}

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧