-1

Well I've got this little script:

<script>
window.addEventListener('deviceorientation', function(event) {
  var a = event.alpha;
  var b = event.beta;
  var g = event.gamma;
}, false);
</script>

<script>
setInterval('alert(""+g+"");', '1000');
</script>

Which should add the deviceorientation, attach it to the page, and alert every 1 second what the orientation currently is, the gamma.

It's not working, though? Nothing alerts! any tips... ideas...?

Edit: I get "Uncaught ReferenceError: g is not defined" in the console log... but it is defined!

user2155738
  • 21
  • 1
  • 4
  • and when I change it to `window.ondevicemotion = function(event) {`, it doesn't work, either – user2155738 Mar 11 '13 at 06:57
  • I can't try anything. I've provided my code, this is not a can I haz the code question. I'm just asking for a fix... are these not allowed on SO any more ? I don't know what to try now! I'm asking for help! – user2155738 Mar 11 '13 at 07:07
  • Why can't you try anything? I downvoted because "This question does not show any research effort; it is unclear or not useful". – BDM Mar 11 '13 at 07:10
  • I did do research effort. I programmed this code, and I'm asking for help with it. How is it unclear ? – user2155738 Mar 11 '13 at 07:11
  • How is it not working? Is there any error codes? Have you tried debugging it? – BDM Mar 11 '13 at 07:13
  • How is it not working ? As I said: "Nothing alerts!" and yes, I have tried debugging it, no error codes – user2155738 Mar 11 '13 at 07:13
  • A downvote for this ? I'm asking for help on a question, and I have tried things!!! – user2155738 Mar 11 '13 at 07:14
  • Oh sorry, I edited my question to clarify. – user2155738 Mar 11 '13 at 07:15
  • You've tried things? Add what you've tried. – BDM Mar 11 '13 at 07:16
  • I can't try anything. It doesn't work. I'm asking for your help. Once it a while, I need help. Isn't this what the site is for ? What have I tried ? I've tried programming this... I'm asking for your help! – user2155738 Mar 11 '13 at 07:17
  • You said you've tried things in an earlier comment, now you're saying that you can't try anything? – BDM Mar 11 '13 at 07:18
  • I've programmed this, alright ? I'm asking you to try things, I don't know what's going on !! This is the whole point of StackOverFlow!!! – user2155738 Mar 11 '13 at 07:19
  • I don't know JavaScript, but I see the problem (defined by Quentin). – BDM Mar 11 '13 at 07:20
  • I know you see the problem! But I didn't! I asked for your help, thank you for offering it!! I didn't know what to try! – user2155738 Mar 11 '13 at 07:25

1 Answers1

2

The var statement sets the scope of a variable to the function it is in.

You are trying to access a local variable in the global scope—where it doesn't exist.

Additionally, you don't set the variable until a deviceorientation occurs, but you try to read it all the time.

You would have to rewrite your code like this:

<script>
window.addEventListener('deviceorientation', function(event) {

  var a = event.alpha;
  var b = event.beta;
  var g = event.gamma;

  console.log( g.toString() ); // Better to use a non-blocking method like console.log to display results.

}, false);
</script>
Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    Wow! I thought `var` was the only way to declare anything!! Wow, I just did `g = VARIABLE INFO HERE;`, and it worked!! in a global scope!! WOWW I never knew this!! – user2155738 Mar 11 '13 at 07:21
  • 2
    @user2155738: Using (declaring) variables in the global scope is something that should be avoided. Instead, use the code above which encloses the variable g in the anonymous function's scope or learn more about how you can use keep variables private using IIFEs and function scope. More on this (just a good start): https://stackoverflow.com/questions/1841916/how-to-avoid-global-variables-in-javascript. Also, always try to stick with let, const or var, when declaring variables. – Kostas Minaidis Sep 30 '18 at 10:54