Why and how on earth will I refer to a variable in console.log to fix the loading error that I had?

It’s hard for me to understand, and I don’t like the “solution”, since it really should not solve my problem.

I have a function that 40% of the time is not fully executed. At first I thought it was due to the fact that I was manipulating the img and iframe elements on the fly, however I did different things to make sure the cache and loading are sorted before they do their job.

In particular, the thing that I did with the newCover variable was performed only in 60% of cases. In my disappointment, I registered a newCover at the end of the function to understand why it just didn't appear sometimes.

But it’s bizarre, because I register a variable with the console, I get a 100% chance of success with a function that does what it should.

Why is this?!

You can see that he is working on the third post here: http://syndex.me

Here is the code:

 albumCover = function(){ $(".forPhotoset").each(function () { var forPhotoset = $(this); var myFrame = forPhotoset.find("iframe"); myFrame.hide(); myFrame.load(function(){ var newCover = myFrame.contents().find('.photoset').children(':first-child').children(':first-child'); newCover.children(':first-child').remove(); var psPre = newCover.attr("href")+ '?' + (Math.random() * 1); newCover.append("<img src='"+ psPre +"' alt='Cover Page' />"); var psHeight = newCover.find('img'); psHeight.load(function(){ if (psHeight.height()>heightRef){ psHeight.height(heightRef); } }) newCover.detach(); forPhotoset.append(newCover); //only executes 60% of the time console.log(newCover);//i added this, now i get 100% success rate?! }) }); } 
+4
source share
1 answer

Just a little guess, but it may happen that calling console.log slows down the script a bit and allows other dom changes to take longer to complete. Perhaps try wrapping your call in setTimeout () to see if it has the same effect as calling console.log.

0
source

Source: https://habr.com/ru/post/1385709/


All Articles