duancaoqin6683 2013-11-07 17:18 采纳率: 0%
浏览 27

为什么javascript忽略了订单?

I have a webpage: index.php. In this page there is a point where i need a custom image that must be generated on the fly.

I have a file: im_gen.php. This file outputs an image using the php image functions. I am sending parameters via $_GET. The image changes when certain events happen and must be regenerated.

I call the file using the <img src=""> property.

Everything works fine. BUT, what i want, is a way to show a loader to the user, while the image is beign generated. I think the best way to do this is to use something like:

document.getElementById("loader").innerHTML = "LOADING" //to show the loader and 
document.getElementById("loader").innerHTML = "DONE"; //to show when the image is generated

I am doing everything it this way:

<?php include 'im_gen.php'; ?>
...
<script>
...
var x = 15;
document.getElementById("loader").innerHTML = "LOADING" //start loading
document.getElementById("image").src = "<?php get_i("+x+"); ?>"; //change the image
document.getElementById("loader").innerHTML = "DONE"; //image change done
...
</script>
...

BUT, for some reason javascript executes the 7th and 8th line at the same time. The result is, Always DONE is show and there is no loader, ever when the image loads.

What I am doing wrong here?

  • 写回答

2条回答 默认 最新

  • duankuai6586 2013-11-07 17:21
    关注

    Your problem is that changing the src of an image tag is what's called asynchronous - it starts the load, and then keeps going, rather than wait for the loading to finish. This means that the innerHTML = "DONE" runs as soon as the load starts, rather than waiting for it to finish. You'll need to use an onload event to change the loader.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本