简体   繁体   English

javascript非阻塞图像加载?

[英]javascript non-blocking image loading?

this function is called every 30ms 每30ms调用一次此函数

function draw(){
    context.clearRect(0, 0,canvas.width, canvas.height);

    context.drawImage(displayme, plot.position.x, plot.position.y, plot.width, plot.height);    
}

and some mouse events modify the displayme.src and plot position 和一些鼠标事件会修改displayme.src和绘图位置

but when the new src is being loaded, all the other javascript gets blocked and cant move the (previously loaded) image 但是当加载新的src时,所有其他javascript被阻止并且无法移动(先前加载的)图像

i've also tried using a different Image object and assigning it to "displayme" but it didn't work 我也尝试过使用其他Image对象并将其分配给“ displayme”,但是它没有用

any help would be most welcome 任何帮助将是最欢迎的

AFAIK all browsers run javascript in a single thread. AFAIK所有浏览器都在单个线程中运行javascript。 You can, using setTimeout() make it so this function returns immediately, but at the time that the function actually runs the browser won't be running any other javascript on the page. 您可以使用setTimeout()进行设置,以便此函数立即返回,但在该函数实际运行浏览器时,该页面上将不会运行任何其他JavaScript。 This simulates multithreading, but it shouldn't be mistaken for the real thing. 这模拟了多线程,但不应将其误认为是真实的东西。

function draw(){
    // allows immediate return and "schedules" the anonymous function next
    setTimeout( function() {
        context.clearRect(0, 0,canvas.width, canvas.height);
        context.drawImage(displayme, plot.position.x, plot.position.y,
                                     plot.width, plot.height);
    },0);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM