views:

22

answers:

2

Hi all,
i had asked a question about refering objects in a function that is inside jquery each function but that first function is defined before in a array.

In my question everything went ok (because i was testing with one element only), when i put 2 elements, it didn't worked, it is calling the first always.

I think that is because ajax is assyncronous.

Can anyone give me a light?

The code is:

var $d=null;
var configs={
  general:{
    selector:'div.MicrodualAdGet',
    max_ads:6},
  logs:{
    selector:'div#MicrodualAdGet-debug'},
  connection:{
    type:'POST',
    url:'http://www.microdual.com/api/microdualgetad',
    cache:false,
    timeout:20000,
    dataType:'json',
    data:{
      adget_id:null,
      client_action:null},
    error:function(r,s,e){MicrodualAdGet_Log("Ajax-Error: "+s+"\n");},
    success: null
  }
};
function MicrodualAdGet_Log(msg){$(configs.logs.selector).append(msg);}
function MicrodualAdGet_View(d,s){
  if(! d) {
    MicrodualAdGet_Log("MicrodualAdGet-Error: Couldn't contact server correctly\n");
    $d.replaceWith("MicrodualAdGet Error: Couldn't contact server correctly");
  }else{
    if(d.hackattemp.status){
      MicrodualAdGet_Log("MicrodualAdGet-Hackattemp-Error: "+d.hackattemp.id+"\n");
      $d.replaceWith("MicrodualAdGet Hackattemp Error: "+d.hackattemp.id);
    }else{
      var content='';
      $d.css({
        display: 'block',
        position: 'relative',
        width: d.tamanhos[d.adpost.id_tamanho].width,
        height: d.tamanhos[d.adpost.id_tamanho].height,
        overflow: 'hidden',
        top: '0px',
        left: '0px',
        background: '#000000'
      })
      if(d.tipos[d.adpost.id_tipo].nome_tipo=='text') content=d.adpost.content;
      if(d.tipos[d.adpost.id_tipo].nome_tipo=='image') content='<a href="'+d.adpost.link+'" target="_blank"><img src="'+d.adpost.content+'" width="'+d.tamanhos[d.adpost.id_tamanho].width+'" height="'+d.tamanhos[d.adpost.id_tamanho].height+'" border="0" /></a>';
      if(content=='') content='MicrodualAdGet Error: Unable to determine ad type. Please contact our <a href="mailto:[email protected]">Network Administrator</a>';
      $d.replaceWith(content);
    }
  }
}



configs.connection.success = MicrodualAdGet_View;
configs.connection.data.client_action = "view";
$(configs.general.selector).each(function(){
  $d=$(this);
  configs.connection.data.adget_id=$(this).attr("rel");
  $.ajax(configs.connection);
});

Thanks in advance,
José Moreira

A: 

Hi there, I'm unable to place comments, but could you provide examples of code.

Jim Grant
He did, just as a link rather than inline; see my comment on the question.
T.J. Crowder
A: 

Fixed, added async:false at config.connection array...

var $d=null;
var configs={
  general:{
    selector:'div.MicrodualAdGet',
    max_ads:6},
  logs:{
    selector:'div#MicrodualAdGet-debug'},
  connection:{
    type:'POST',
    url:'http://www.microdual.com/api/microdualgetad',
    cache:false,
    async:false,
    timeout:20000,
    dataType:'json',
    data:{
      adget_id:null,
      client_action:null},
    error:function(r,s,e){MicrodualAdGet_Log("Ajax-Error: "+s+"\n");},
    success: null
  }
};
function MicrodualAdGet_Log(msg){$(configs.logs.selector).append(msg);}
function MicrodualAdGet_View(d,s){
  if(! d) {
    MicrodualAdGet_Log("MicrodualAdGet-Error: Couldn't contact server correctly\n");
    $d.replaceWith("MicrodualAdGet Error: Couldn't contact server correctly");
  }else{
    if(d.hackattemp.status){
      MicrodualAdGet_Log("MicrodualAdGet-Hackattemp-Error: "+d.hackattemp.id+"\n");
      $d.replaceWith("MicrodualAdGet Hackattemp Error: "+d.hackattemp.id);
    }else{
      var content='';
      $d.css({
        display: 'block',
        position: 'relative',
        width: d.tamanhos[d.adpost.id_tamanho].width,
        height: d.tamanhos[d.adpost.id_tamanho].height,
        overflow: 'hidden',
        top: '0px',
        left: '0px',
        background: '#000000'
      })
      if(d.tipos[d.adpost.id_tipo].nome_tipo=='text') content=d.adpost.content;
      if(d.tipos[d.adpost.id_tipo].nome_tipo=='image') content='<a href="'+d.adpost.link+'" target="_blank"><img src="'+d.adpost.content+'" width="'+d.tamanhos[d.adpost.id_tamanho].width+'" height="'+d.tamanhos[d.adpost.id_tamanho].height+'" border="0" /></a>';
      if(content=='') content='MicrodualAdGet Error: Unable to determine ad type. Please contact our <a href="mailto:[email protected]">Network Administrator</a>';
      $d.replaceWith(content);
    }
  }
}



configs.connection.success = MicrodualAdGet_View;
configs.connection.data.client_action = "view";
$(configs.general.selector).each(function(){
  $d=$(this);
  configs.connection.data.adget_id=$(this).attr("rel");
  $.ajax(configs.connection);
});

Thank you all...

CuSS