Home>

I want to change the character displayed by conditional branching in the function executed at setInterval

Please write your question here in detail.
I want to change the characters displayed according to the direction of the camera in the A-frame.
For that purpose, we first tested whether the display could be changed by conditional branching on the console.
"Good morning" is always displayed on the console, and conditional branching is not well done.

Error message

No error has occurred.

Applicable source code
<! DOCTYPE html>
<html lang = "ja" dir = "ltr">
  <head>
    <meta charset = "utf-8">
    <title>Sample A-frame</title>
    <script src = "https://aframe.io/releases/0.7.1/aframe.min.js"></script>
  </head>
  <body>
    
      
        </a-cursor>
      </a-camera>
    </a-scene>
    <script type = "text/javascript">
    // get a-camera
      var camera = document.getElementById ('a-camera');
      function aisathu () {
        // Get the rotation element of a-camera
        var rotation = camera.getAttribute ('rotation');
        var Rot = rotation.y;
        console.log (Rot);
        // If camera turns 1-45 degrees
        if (0<Rot<46) {console.log ("Good morning");}
        // If the camera turns 4 to 90 degrees
        else if (45<Rot<91) {console.log ( "Hello");}
        // If camera turns 91-180 degrees
        else if (91<Rot<181) {console.log ("Goodbye");}
        else if (180<Rot<270) {console.log ("Good night");}
      }
      setInterval (aisathu, 1000);
    </script>
  </body>
</html>

I made rotation a global variable and tried conditional branching outside the function.
rotation.y was returned to aisathu function.

Supplemental information (FW/tool version etc.)

We use setInterval to always update the camera orientation.

  • Answer # 1

    Rewrite0instead of0.

  • Answer # 2

    I want to know what the behavior of the variable Rot looks like in console.log
    I think that the angle of the camera is substituted for the variable Rot and it is displayed, but there is no possibility that the output specification on the camera side will output only once when the angle changes or such specification ?

    As a possibility, as I pointed out by kei344, I wonder if the writing of if is causing problems ...
    What happens if you try the following?

    if (Rot>0&&Rot<46) {console.log ("Good morning");}
            // If the camera turns 4 to 90 degrees
            else if (Rot<91) {console.log ( "Hello");}
            // If camera turns 91-180 degrees
            else if (Rot<181) {console.log ("Goodbye");}
            else if (Rot<270) {console.log ("Good night");}