Unexplained changes if I add a “warning”,

please help on this issue: Inside the loop, I first call a function that will change the contents of the DIV, and then try to display the new content. Code (in brief):

while (...){
    fetch(website_url);
//  alert("useless");
    x=document.getElementById("prova").textContent;
    alert(x);
}

"Fetch" is a function that uses jQuery to open the specified URL and paste content into the "prova" div. See Comment "alert"? If I continue to comment on it, the “fetch” works fine (I see the extracted code in the browser window), but the second warning does not work! It shows the contents of the PREVIOUS DIV (i.e. the first time it is empty, the contents of the first iteration, etc. will be displayed in the second iteration). However, if I uncomment the first warning, the second warning works fine. This clearly looks like a synchronization problem, but I tried many ways (i.e., with timeouts, ifs, ..), and I could not solve it. Any suggestion?

Here is the fetch function (actually it's not mine ... I'm just learning how to use it)

function fetch(url){
  container = $('#prova');
  doAjax(url);

  function doAjax(url){
    $.getJSON("http://query.yahooapis.com/v1/public /yql?"+"q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent(url)+"%22&format=xml'&callback=?",
function(data){
    if(data.results[0]){
      var data = filterData(data.results[0]);
      container.html(data);
    } else {
      var errormsg = '<p>Error: could not load the page.</p>';
      container.html(errormsg);
    }
  }
}
+3
3

, , , AJAX : Yahoo, , , , script, . .

, , alert, , , , .

AJAX ( "AJAX" " Javascript XML" , XmlHttpRequest):

var url = "http://query.yahooapis.com/v1/public /yql?"+"q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent(url)+"%22&format=xml'&callback=?";

// Your asynchronous call:
$.getJSON(url, function(data) { ... });

// A synchronous alternative:
$.ajax({
    type: 'GET',
    url: url,
    dataType: 'json',
    data: {},
    async: false,
    success: function(data) { ... },
  });

( ) , .

, , , , :

while (...) {
    function myFunction() {
        var x = document.getElementById("prova").textContent;
        alert(x);
    }
    fetch_and_handle(website_url, myFunction);
}

function fetch_and_handle(url, handler) {
    var container = $('#prova');
    var url       = "http://query.yahooapis.com/v1/public /yql?"+"q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent(url)+"%22&format=xml'&callback=?";

    $.getJSON(url, function(data) {
       if (data.results[0]) {
          var data = filterData(data.results[0]);
          container.html(data);

          if (handler != undefined) {
              handler();
          }
       }
       else {
          var errormsg = '<p>Error: could not load the page.</p>';
          container.html(errormsg);
       }
   }
}

var : , var. .

+1

. , .

, jQuery ajax , .

$.get('ajax/test.html', function(data) {
  $('.result').html(data);
  alert('Load was performed.');
});

. http://api.jquery.com/jQuery.get/

+1

, , . , :

x=document.getElementById("prova").textContent;

ajax-call (fetch()) . "" , ajax-call , , "ok", .

, :

  • change your synchronization request (but this is a bad idea almost every time)
  • use the success callback of your ajax request to execute code that should wait for the request to complete.

EDIT: you did not post your code for fetch(), so the question is that you are using some jquerys ajax functions there ... posting the full code would be nice.

0
source

All Articles