views:

148

answers:

4

I am using this:

//if in landscape mode
while(window.orientation == 90 || window.orientation == -90) {
                    if(!$('#page' + hiddenNextPage).hasClass('showUp')) {
                        $('#page' + hiddenNextPage).addClass('showUp');
                        $('#page' + hiddenNextPage).css('margin-left', '300px');
                    };
                }

//if in portrait mode
while(window.orientation == 0) {
                    if($('#page' + hiddenNextPage).hasClass('showUp')) {
                        $('#page' + hiddenNextPage).removeClass('showUp');
                        $('#page' + hiddenNextPage).css('margin-left', '5px');
                    };
                };

But its making my pages not even load anymore.. & it takes such a long time to load this. Is there anything wrong with it?

Is there a better way of constantly checking if the orientation has been changed without using a while loop?

This is for the ipad/iphone
Thanks!

+5  A: 

Since you're only changing styles, you could use separate stylesheets for portrait and landscape...

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">

EDIT

There's also an onorientationchange event on window which allows you to run code when the orientation changes rather than constantly polling for a change...

window.onorientationchange = function (event)
{
    if (window.orientation == 90 || window.orientation == -90)
    {
        ...
    }
    else
    {
        ...
    }
}
stevemegson
I am using that also, but in landscape mode I need a completely new div inserted. Soo I need to find some way with javascript/jquery. There will also be a lot of changes to the original code after wards.
cat
Thank you that seems perfect! ill try it out now
cat
Working great, Thanks :)
cat
+6  A: 

I don't see that the bodies of your while loops are modifying the while tests. This produces an infinite loop.

jhurshman
I'm not sure what you mean but I want an infinite loop.I need it to continue checking it to see if it has been changed.
cat
The result of the test will change when the orientation of the device changes, so the apparently infinite loop is a deliberate way to wait for the change of orientation. However, it's also the cause of the slow loading problem since it will consume 100% CPU constantly testing for a change.
stevemegson
There's a really big difference between an actual "infinite loop", in programmatic terms, and a "periodic algorithm, executed indefinitely". What you have in your code above is the former (which will lock the browser), and what you need is the latter - of which several SOers have offered solutions.
Peter Bailey
Thanks, I ended up using stevemegson's way it was much easier.
cat
+2  A: 

I'd recommend seperate style sheets, but as far your question of constantly checking you can set a timeout with setInterval ( expression, interval );

example:

setInterval(function() {
   if ((window.orientation == 90 || window.orientation == -90) {
      if(!$('#page' + hiddenNextPage).hasClass('showUp')) {
         $('#page' + hiddenNextPage).addClass('showUp');
         $('#page' + hiddenNextPage).css('margin-left', '300px');
      }
   } else if (window.orientation == 0) {
      if($('#page' + hiddenNextPage).hasClass('showUp')) {
         $('#page' + hiddenNextPage).removeClass('showUp');
         $('#page' + hiddenNextPage).css('margin-left', '5px');
      }
   }
}
, 2000 );

edit Accidently used setTimeout instead of setInterval the first time, oops.

Robert
I think you mean `setInterval`
Peter Bailey
Bah, yeah I did, thanks.
Robert
+3  A: 

I don't think you should be using a while loop at all.

If you want to execute code periodically, use a timeout or interval.

Also, your jQuery is very inefficient - especially with how you re-run the selector so many times.

var $nextPage = $('#page' + hiddenNextPage );

window.setInterval( function()
{
  if ( 0 == window.orientation )
  {
    if ( $nextPage.hasClass( 'showUp' ) )
    {
      $nextPage
        .removeClass( 'showUp' )
        .css( 'margin-left', '5px' )
      ;
    }
  }
  else if ( !$nextPage.hasClass( 'showUp' ) )
  {
    $nextPage
      .addClass( 'showUp' )
      .css( 'margin-left', '300px' )
    ;
  }
}, 100 ); // 100 means this will execute every 100 milliseconds
Peter Bailey
Oh, i only repeated it to make it more simple to read instead of putting it all in one big line. I didn't know i could do it the way you just did though! thanks
cat