English 中文(简体)
我如何设定一个函数, 使编程线将等到编程线完成, 并移动到下一行的 javastramp 。
原标题:How do i make a function so that the programming line will wait until it s done and move to the next line in javascript

I m creating an HTML5 with Javascript, but sometimes the code is executing the next line of statement before the current line is finished,

For example, I m passing an image to an object in Javascript, but then the next statement is executed already before the object has the image, so sometimes the object has no image, but sometimes it has,

So between the 2 lines I need to assign something like callback or wait function, but i dont know how

var image=new Image();
ws.send("complete");
最佳回答

Javascript 是( 主要是) 零星的, 这是件好事 。 您不想在等待图像时屏蔽 UI 。

在图像完成后执行 ws. send 时使用以下方法执行 :

var image = new Image();
image.onload = function()
{
 ws.send("complete");
}
image.src =  image.jpg ;

如果你这样做同步,整个程序会阻断, 你不想这样做。

至于回调,您可以将函数作为参数传递到函数中。例如:

function doSomething(callback)
{       
    callback();
}

doSomething(function()
{
 console.log( im done! );
});
问题回答

Not sure if your question is image specific, but for images, you can do the following: The onload event will fire once the image is loaded.

var im = new Image();
im.onload = function() { ws.send( complete ); }
im.src =  path/to/image.jpg 




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签