document.write()与setTimeout()

本文介绍了一种使用JavaScript创建动态时钟的方法,并解决了document.write()导致页面不断被重写的问题,通过更改输出方式使时钟能够正常运行。
刚学习Javascript不太了解document.write()函数

在编写时钟脚本时候遇到了问题
<script> 
function print(){
var my=new Date()
document.write(my.toLocaleString())
t=setTimeout('print()',500)
}
</script>


本来期待时钟会自己走,但是并没有反应

查询资料知道这个document.write()函数会重写页面,也就是这段代码都直接写掉了,因此也就不会递归执行自己了,将document.write(my.toLocaleString())改为document.body.innerHTML=my.toLocaleString()+"<br>"即可,当然了这段代码要写到head标签里。
[Violation]Avoid using document.write(). [Violation]Avoid using document.write(). [Violation]Avoid using document.write(). [Violation]Avoid using document.write(). [Violation]Avoid using document.write(). [Violation]Avoid using document.write(). [Violation]Avoid using document.write(). [Violation]Avoid using document.write(). SuperMap.Include.js:1 [Violation]Parser was blocked due to document.write(<script>) i @ SuperMap.Include.js:1 (匿名) @ SuperMap.Include.js:1 (匿名) @ SuperMap.Include.js:1 SuperMap.Include.js:1 [Violation]Parser was blocked due to document.write(<script>) i @ SuperMap.Include.js:1 (匿名) @ SuperMap.Include.js:1 (匿名) @ SuperMap.Include.js:1 SuperMap.Include.js:1 [Violation]Parser was blocked due to document.write(<script>) i @ SuperMap.Include.js:1 (匿名) @ SuperMap.Include.js:1 (匿名) @ SuperMap.Include.js:1 :9933/#/login:1 [Intervention]Slow network is detected. See https://www.chromestatus.com/feature/5636954674692096 for more details. Fallback font will be used while loading: http://192.168.1.203:9933/static/b462afa2d5b52ed52701.otf 2030.79df9936.js:2 production stomp.min.js:1 Opening Web Socket... 2030.79df9936.js:2 $keepAliveViews Proxy(Array) {} 2030.79df9936.js:2 {fullPath: '/login', path: '/login', query: {…}, hash: '', name: 'Login', …} '------------------' 2030.79df9936.js:2 $keepAliveViews Proxy(Array) {} :9933/#/login:1 [Intervention]Slow network is detected. See https://www.chromestatus.com/feature/5636954674692096 for more details. Fallback font will be used while loading: http://192.168.1.203:9933/static/150b8ee34940fba3a3d1.otf :9933/#/login:1 Access to XMLHttpRequest at 'https://192.168.1.203:8084/endpointOyzc/info?t=1751355305204' from origin 'http://192.168.1.203:9933' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://192.168.1.203:9933, *', but only one is allowed. 2030.79df9936.js:2 GET https://192.168.1.203:8084/endpointOyzc/info?t=1751355305204 net::ERR_FAILED 200 (OK) r._start @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 setTimeout r @ 2030.79df9936.js:2 n @ 2030.79df9936.js:2 n @ 2030.79df9936.js:2 n._getReceiver @ 2030.79df9936.js:2 n.doXhr @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 setTimeout n @ 2030.79df9936.js:2 r @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 e @ 2030.79df9936.js:2 29863 @ 2030.79df9936.js:2 o @ runtime.0a6e0595.js:1 (匿名) @ 2030.79df9936.js:2 Promise.then 1863 @ 2030.79df9936.js:2 o @ runtime.0a6e0595.js:1 Promise.then 68138 @ main.f0bca0c4.js:1 o @ runtime.0a6e0595.js:1 (匿名) @ main.f0bca0c4.js:1 a @ runtime.0a6e0595.js:1 (匿名) @ main.f0bca0c4.js:1 stomp.min.js:1 Whoops! Lost connection to https://192.168.1.203:8084/endpointOyzc 2030.79df9936.js:2 Establish error: Whoops! Lost connection to https://192.168.1.203:8084/endpointOyzc config @ 2030.79df9936.js:2 (匿名) @ stomp.min.js:1 n.dispatchEvent @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 setTimeout r._close @ 2030.79df9936.js:2 r._receiveInfo @ 2030.79df9936.js:2 i @ 2030.79df9936.js:2 n.emit @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 i @ 2030.79df9936.js:2 n.emit @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 i @ 2030.79df9936.js:2 n.emit @ 2030.79df9936.js:2 xhr.onreadystatechange @ 2030.79df9936.js:2 XMLHttpRequest.send r._start @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 setTimeout r @ 2030.79df9936.js:2 n @ 2030.79df9936.js:2 n @ 2030.79df9936.js:2 n._getReceiver @ 2030.79df9936.js:2 n.doXhr @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 setTimeout n @ 2030.79df9936.js:2 r @ 2030.79df9936.js:2 (匿名) @ 2030.79df9936.js:2 e @ 2030.79df9936.js:2 29863 @ 2030.79df9936.js:2 o @ runtime.0a6e0595.js:1 (匿名) @ 2030.79df9936.js:2 Promise.then 1863 @ 2030.79df9936.js:2 o @ runtime.0a6e0595.js:1 Promise.then 68138 @ main.f0bca0c4.js:1 o @ runtime.0a6e0595.js:1 (匿名) @ main.f0bca0c4.js:1 a @ runtime.0a6e0595.js:1 (匿名) @ main.f0bca0c4.js:1 :9933/#/login:1 [DOM] Password field is not contained in a form: (More info: https://www.chromium.org/developers/design-documents/create-amazing-password-forms) <input type=​"password" name=​"password" class=​"IZY4NP9Uc0CXwohzTFYD" placeholder=​"请输入密码">​ stomp.min.js:1 Opening Web Socket...
最新发布
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值