JavaScript Compute div style only when it completes rendering an image

In my script I am loading an image from an array into a div, then I calculate some style elements to be tuned. The problem is that this works only if I use a timeout function before calculating the styles, like this:

  $('#forecastImg').attr('src',get_ImageItemLocation(imageArray[0]));

  setTimeout(function(){ 
       var himg= $("#forecastImg").height();
       var hest = $("#esterno").height();
       var margin= ((hest-himg)/2)-$(".header").height()-$(".forecastdate").height();
       if (margin>0){
         $("#forecastImg").css('margin-top',(margin+'px'));
      }
  },240);

How can I get rid of the timeout and be sure that the height() value is correct? If I remove the timeout, I alwyas get a height()=0 value.

Answer:1

Well the problem here is that the image is not loaded when you're trying do calculate the margin, that's why $('#forecastImg').height() is not returning the expected height when you try to make the calculations right away (in your case, you're doing a wait for the loading with a setTimeout)

However, you can use $().load method instead of a unreliable setTimeout to consistently run your calculations after the loading of the image. To do that, you must first bind the .load callback method with those calculations and only then change the src value of it, otherwise the image might load immediately without calling our .load callback:

  $('#forecastImg')
      .one('load', function () { 
           var himg = $("#forecastImg").height(),
               hest = $("#esterno").height(),
               margin = ((hest-himg) / 2) - $(".header").height() - $(".forecastdate").height();

           if (margin > 0) {
               $("#forecastImg").css('margin-top',(margin+'px'));
           }
      })
      .attr('src', get_ImageItemLocation(imageArray[0]));
Answer:2

You need to wait for the image has loaded, so the timeout is correct, but if you would like to use a different solution, you can use load event instead:

$("#forecastImg").one("load", function() {
       var himg= $(this).height();
       var hest = $("#esterno").height();
       var margin= ((hest-himg)/2)-$(".header").height()-$(".forecastdate").height();
       if (margin>0){
         $("#forecastImg").css('margin-top',(margin+'px'));
      }
});
Answer:3

This might be a question more for Jasmine or Angular, but I’m wondering how to use the Loopback-generated Angular services in my tests. I don’t need to test the services themselves, but I need to test ...

This might be a question more for Jasmine or Angular, but I’m wondering how to use the Loopback-generated Angular services in my tests. I don’t need to test the services themselves, but I need to test ...

  1. loopback services angular

How to transform "t=1&m=6&r=2" to {t:1, m:6, r:2} by lodash?

How to transform "t=1&m=6&r=2" to {t:1, m:6, r:2} by lodash?

  1. transform string into list python
  2. transform string into datetime python
  3. transform string into number javascript
  4. transform string into array
  5. transform string into number python
  6. transform string into list
  7. transform string into array javascript
  8. transform string into date javascript
  9. transform string into numeric stata
  10. transform string into json python
  11. transform string into dict python
  12. transform string into array php
  13. transform string into json
  14. transform string into array python
  15. transform string into array js
  16. transform string into int java
  17. transform string into int
  18. transform string into int c++
  19. transform string into variable python
  20. transform string into date stata

I was testing this code out in an asp.net webform as I am looking to split a text into a list or words in javascript and have each added to its own div. http://www.w3schools.com/jsref/tryit.asp?...

I was testing this code out in an asp.net webform as I am looking to split a text into a list or words in javascript and have each added to its own div. http://www.w3schools.com/jsref/tryit.asp?...

  1. javascript split text into lines
  2. javascript split text by newline
  3. javascript split text by space
  4. javascript split text by comma
  5. javascript split text file into lines
  6. javascript split text by delimiter
  7. javascript split text by line break
  8. javascript split text by length
  9. javascript split text into sentences
  10. javascript split text by regex
  11. javascript split text into paragraphs
  12. javascript split text over multiple lines

new javascript student here looking for a little clarification. I keep running into problems involving For Loops/Arrays. I'm not familiar enough with the concepts to diagnose my own problems. The ...

new javascript student here looking for a little clarification. I keep running into problems involving For Loops/Arrays. I'm not familiar enough with the concepts to diagnose my own problems. The ...

  1. return string in c
  2. return string in java
  3. return string in python
  4. return string in javascript
  5. return string in c function
  6. return string in actionresult
  7. return string in cpp
  8. return string in python function
  9. return string in php
  10. return string in async task c#
  11. return string with variables python
  12. return string and int java
  13. return string in shell script
  14. return string in json format c#
  15. return string in javascript function
  16. return string to view mvc
  17. return string in js
  18. return string in bash
  19. return string in java method
  20. return string and int python