简体   繁体   English

下载文件会阻止 Javascript 事件吗?

[英]Downloading file blocks Javascript events?

I am creating an .Net library with built-in WebBrowser control.我正在创建一个带有内置 WebBrowser 控件的 .Net 库。 The library is generally supposed to be handling google maps in other applications.该库通常应该在其他应用程序中处理谷歌地图。 Basically all essential Google maps API events are sent to my C# lib to be passed on to app using it.基本上所有基本的 Google 地图 API 事件都会发送到我的 C# 库,以传递给使用它的应用程序。

Everything works great until application using my lib wants to change marker icon on click and perform different action on double click.一切正常,直到使用我的 lib 的应用程序想要在单击时更改标记图标并在双击时执行不同的操作。 In this case, application changes marker icon on C# level, which in turn invokes an Javascript function to change the icon on the map:在这种情况下,应用程序在 C# 级别更改标记图标,进而调用 Javascript 函数来更改地图上的图标:

function SetMarkerIcon(id, imageUri) {
    markers[id].setIcon(imageUri);
 }

I debugged the problem to a conclusion that Javascript freezes until image file is downloaded and becomes unresponsive to a second click, which is supposed to trigger dblclick event.我调试了这个问题,得出的结论是 Javascript 冻结,直到下载图像文件并且对第二次点击没有响应,这应该触发dblclick事件。 So my first question is if this is correct way of thinking.所以我的第一个问题是这是否是正确的思维方式。

Based on this assumption I tried to delay downloading of a file a bit by using setTimeout :基于这个假设,我尝试使用setTimeout延迟下载文件:

function SetMarkerIcon(id, imageUri) {
    setTimeout(function () { markers[id].setIcon(imageUri); }, 200);
 }

but obviously this solution is very 'dirty' (although works in test environment, in fact it worked on even 0ms delay while using local .js files, maybe because of a kind of separate thread setTimeout uses, but not with scripts on server), thus the second question: do you have any usable ideas how to solve this bug properly?但显然这个解决方案非常“肮脏”(尽管在测试环境中有效,实际上它在使用本地 .js 文件时甚至延迟了 0 毫秒,这可能是因为使用了一种单独的线程setTimeout ,但不适用于服务器上的脚本),因此第二个问题:你有什么可用的想法如何正确解决这个错误?

Sorry if question is a bit vague.对不起,如果问题有点模糊。

You can switch the marker's icon very fast by using sprites.您可以使用精灵非常快速地切换标记的图标。 You store all your icons as sprites in one file on regular grid positions.您将所有图标作为精灵存储在一个文件中,位于常规网格位置。 You load only once this file into the browser.您只需将此文件加载到浏览器中一次。 You switch the icon by changing the origin of the sprite.您可以通过更改精灵的原点来切换图标。 Here is a prototypical implementation:这是一个典型的实现:

var pos = new google.maps.LatLng(0, 0);
var url = "/.../sprites.png";             // file with all icons as sprites
var size = new google.maps.Size(24, 24);  // size of 1 sprite
var origin = new google.maps.Point(0, 0);
var anchorPos = new google.maps.Point(12, 12);
var icon = new google.maps.MarkerImage(url, size, origin, anchorPos);
var marker = new google.maps.Marker({       
    position: new google.maps.LatLng(0, 0),       
    map: map,     
    icon: icon
}); 

marker.getIcon().origin = new google.maps.Point(5 * 24, 0);  // set the icon to the 5'th sprite

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

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