Option 1

window.urlSite=$(this).attr('href'); b1 = $.ajax($(this).attr('href')); b1.done(function (d1) { analysisSite(d1,mask2,window.urlSite); }); 

Option 2

 b2 = $(this).attr('href'); b1 = $.ajax($(this).attr('href')); $.when( b1, b2 ).done(function ( d1, d2 ) { analysisSite(d1,mask2,d2); }); 

in the first one, the analysisSite () function is running normally. In the second - the alert says that the page is received. But for some reason the code does not want to work further with it.

I need the second option, in addition to the requested page, to provide a link to this page. The first option does not transmit this information.

  • one
    What is this in this case? The calls are different: in the first case, it is transmitted to window.urlSite , in the second, $(this).attr('href') , if these values ​​do not match, it is not surprising that they work differently. More specifically, one can only say after seeing how all this is called, and what is inside the analysisSite - Grundy
  • @ Grundy, I'm sorry I missed it. I added the question code. window.urlSite in version 1 is the same $ (this) .attr ('href'); This is what the $ (data) .find ("h3") function gives us. Each (function () {this is part of the analysisSite () function, that is, analysisSite () calls itself until it finds what it is looking for or does not end the link tree - Michael

1 answer 1

The difference is that it is passed to the done function.

In the case of a call to $.when with one argument, the parameters of the done callback will be the results of the promise given in $.when .

For example:

 $.when( $.ajax(...) ).done(function ( result, status, xhr) { ... } ); 

In case there are several parameters:

 $.when( $.ajax(...), var1, var2 ) 

Each parameter of a callback will correspond to the result of a specific promise:

 $.when( $.ajax(...), var1, var2 ).done(function(res1, res2, res3){ // res1 - массив из трех элементов `[result, status, xhr]` - результат выполнения `ajax`. // res2 - результат выполнения var1 // res3 - результат выполнения var2 }); 

Since in your case, the second parameter is a string, it will also be the result.

As a result, in order to get both results, you need to do this:

 $.when( b1, b2 ).done(function ( d1, d2 ) { var ajaxResult = d1[0]; var stringResult = d2; analysisSite(ajaxResult, mask2, stringResult); }); 
  • EESS !!!))) Earned! How much I danced around it) Thanks is huge my thanks to you!)) Yes, but in my case it is better to do without declaring var variables and immediately throw d1 [0] and d2 into the function. - Michael
  • @ Mikhail, I selected the variables so that it was clearly visible where we are coming from. It should be, by the way, to work on names, and then you will remember for a long time what b1 is and what d2 is :-) - Grundy
  • I have no complaints!) That's right, Many thanks!) - Michael