JavaScript Javascript causes browser to hang

Even though console log give the while expression as false the loop runs infinetly.

var tile_height;
$(document).ready(function(){
tile_height = $("#department > .front").height();
        });
        function tile_animate(e){
        console.log(($('> .front',e).css("height") > '0px') && ($('> .back',e).height() < tile_height));
        while(($('> .front',e).css("height") > '0px') && ($('> .back',e).height() <  tile_height)) 
            {
                $('> .front',e).animate({height: '-=1px'});
                $('> .back',e).animate({height: '+=1px'});
            }
        }

I have tried it using if statement as a recursive function but that too does not work. Using while causes browser to stop responding. The html code that calls the function is

HTML code

<div class="tiles" id="gallery" onmouseover="javascript:tile_animate(this)">
    <div class="front">
    </div>
    <div class="back">
    </div>
</div>
Answer:1

The issue is that you are using a loop while hovering. Better, is that you use the jQuery animate in a style and apply it when necessary.

However, the way you have described the problem, it looks like you will shrink the div once - there is nothing to restore it afterwards. I noticed that the div was named 'department' but the JavaScript was looking for 'gallery' - so I changed that...

So, to perfrom this as you have your code now, a few tweaks are in order:

Remove the while loop and perform a check instead while hovering.

The check is to see if the animation is already occurring.

If the animation is occurring, ignore the hover.

If the animation is not already occurring, set a flag to say it is, and perform the required animation - not reduce the size of the div pixel by pixel (this is what the animation does for you).

When done, the div will have a height of 0px (and cannot be hovered over again).

If you wanted to restore the div later, then store its original dimensions prior to the animation, and restore them when needed (not forgetting to reset the inLoop variable)...

var tile_height;
var inLoop = false;
$(document).ready(function() {
  tile_height = $("#gallery > .front").height();
});

function tile_animate(e) {
  if (!inLoop && ($('> .front', e).css("height") > '0px') && ($('> .back', e).height() < tile_height)) {
    inLoop = true;
    $('> .front', e).animate({
      height: '0px'
    });
    $('> .back', e).animate({
      height: tile_height
    });
  }
}
Answer:2

Javascript is single threaded. In particular your code has to finish execution before another thread can start. The other thread you are really interested in is Jquery's animation loop which by default loops on a 14 ms timer thread.

In consequence your while loop is running infinitely because animation code never runs to decrement the height you are testing and expect to reach zero.

Answer:3

Short question. How does one find the dimensions of a mesh in three.js? I have Collada (.dae) files which I would like to know the size of in units (x,y,z). I have seen comments about using geometry....

Short question. How does one find the dimensions of a mesh in three.js? I have Collada (.dae) files which I would like to know the size of in units (x,y,z). I have seen comments about using geometry....

Within the code below contains a very simple question game (work-in-progress). So far, I've been able to have a "Rules" message appear through the onLoad function, "qDisplayLoad()". Next, the onClick ...

Within the code below contains a very simple question game (work-in-progress). So far, I've been able to have a "Rules" message appear through the onLoad function, "qDisplayLoad()". Next, the onClick ...

  1. splice object from array
  2. splice object from array typescript
  3. splice object from array angular 2
  4. splice object from array js
  5. splice object from array angularjs
  6. splice object from array jquery
  7. splice from object
  8. splice object in javascript
  9. splice object in angularjs
  10. splice object in c++
  11. splice specific object from array
  12. splice remove object from array
  13. splice json object from array
  14. object splice in js
  15. splice not removing object from array
  16. javascript splice remove object from array

So basically when i hover over "drive" option which is a h1, i want a div container to appear that contains several other in detail options. Then if i click on "ride" option which is another h1, i ...

So basically when i hover over "drive" option which is a h1, i want a div container to appear that contains several other in detail options. Then if i click on "ride" option which is another h1, i ...

  1. change style element javascript
  2. change style element jquery
  3. change style element js
  4. change style element
  5. change style element angular
  6. change style element angularjs
  7. change element style css
  8. change element style react
  9. change element style typescript
  10. change element style using javascript
  11. change element style angular 4
  12. change element.style wordpress
  13. change element style in iframe
  14. change element style on click
  15. change element style class javascript
  16. change element style inside iframe
  17. change element style html
  18. change element style using jquery
  19. change element style color
  20. change style dom element

I have something like this: = f.input :payment_date, as: :date = f.input :tuitor, as: :boolean, label: 'Tuitor payment' and I want use on this a simple JavaScript like this: costam = -> if ...

I have something like this: = f.input :payment_date, as: :date = f.input :tuitor, as: :boolean, label: 'Tuitor payment' and I want use on this a simple JavaScript like this: costam = -> if ...