简体   繁体   English

将xml载入div时的AJAX竞争条件

[英]AJAX race condition when loading xml into div

The website that I am creating has information that is used on multiple pages. 我正在创建的网站具有在多个页面上使用的信息。 The way I have it setup is that the information is stored in xml files. 我设置的方式是将信息存储在xml文件中。 I then use AJAX to load the xml files and on success I call a parsing method that appends th. 然后,我使用AJAX加载xml文件,并在调用成功后调用附加的解析方法。

It seems that there is a race condition happening. 似乎正在发生比赛状况。 The page finishes loading before the xml file is finished parsing. 在xml文件完成解析之前,页面已完成加载。 If I add an alert in the parsing method, then it works fine (which is why I think its a race condition). 如果我在解析方法中添加了警报,则它可以正常工作(这就是为什么我认为它是一种竞争条件)。

function loadXML(link) {

$('#div1').load(link);
loadImage(link);

if (link == "news.html") {
    $.ajax({
        type: "GET",
        url: "news.xml",
        dataType: "xml",
        success: newsParser
     });
  }
}

When I've searched online I've seen things about maybe using a semaphore or timeout. 当我在线搜索时,我已经看到有关使用信号灯或超时的信息。 Neither of these seemed to work for me very well. 这些似乎都不适合我。

If the html page isn't loaded before the xml then there is no div to add the information to. 如果未在xml之前加载html页面,则没有div可以添加信息。 I also tried to load the different xml files when you go to the page and store the data so that it isn't parsed every time you go to the specific page but I wasn't successful at figuring that out. 当您转到该页面时,我还尝试加载不同的xml文件并存储数据,以便每次访问特定页面时都不会对其进行解析,但是我并没有成功地弄清楚这一点。

I appreciate any help provided! 感谢提供的任何帮助!

You can wait for the DOM to load using jQuery (which you're already using): $().ready() - http://api.jquery.com/ready/ 您可以使用jQuery(已经在使用)等待DOM加载: $().ready() -http://api.jquery.com/ready/

$(document).ready(function(){ 
  $('#div1').load(link); 
  loadImage(link); 
  //etc..
}

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

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