1

I'm trying to let the code create a data file for my marzipano project, it uses this data.js and I don't want to create every link for each project so I tried to loop it but it doesnt print it into my html page. I want to print it as a text so I can copy and paste the result in my js file, is there a way to fix my code or a better way to do it?

P.S: I'm a total noob with javascript

Thank you in advance

function auto(number){
 i = 0;
 while (i < number) {
  //Fist Scene
  if(i === 0){
   document.write('
   <p>    
    {
      "id": "0",
      "name": "0",
      "levels": [
     {
       "tileSize": 256,
       "size": 256,
       "fallbackOnly": true
     },
     {
       "tileSize": 512,
       "size": 512
     },
     {
       "tileSize": 512,
       "size": 1024
     },
     {
       "tileSize": 512,
       "size": 2048
     }
      ],
      "faceSize": 2000,
      "initialViewParameters": {
     "yaw": -3.0907815953112916,
     "pitch": 0.06648956035942888,
     "fov": 1.5707963267948966
      },
      "linkHotspots": [
     {
       "yaw": -3.128953846954726,
       "pitch": 0.47317799909128944,
       "rotation": 0,
       "target": "1"    
     }
      ],
      "infoHotspots": []
    },</p>
    ')
  }
  
  //Last Scene
  else if (i === number){
   document.write('
   <p>
   {
     "id": "'i'",
     "name": "'i'",
     "levels": [
    {
      "tileSize": 256,
      "size": 256,
      "fallbackOnly": true
    },
    {
      "tileSize": 512,
      "size": 512
    },
    {
      "tileSize": 512,
      "size": 1024
    },
    {
      "tileSize": 512,
      "size": 2048
    }
     ],
     "faceSize": 2000,
     "initialViewParameters": {
    "yaw": -3.1332154632455715,
    "pitch": 0.062442602034723294,
    "fov": 1.5707963267948966
     },
     "linkHotspots": [
    {
      "yaw": 0.008275683165861025,
      "pitch": 0.3876084470351344,
      "rotation": 0,
      "target": "'i-1'"
    }
     ],
     "infoHotspots": []
   }</p>'
   )
  }
  
  //Actual loop
  else if (i < number){
   document.write('
   {
     "id": "i",
     "name": "i",
     "levels": [
    {
      "tileSize": 256,
      "size": 256,
      "fallbackOnly": true
    },
    {
      "tileSize": 512,
      "size": 512
    },
    {
      "tileSize": 512,
      "size": 1024
    },
    {
      "tileSize": 512,
      "size": 2048
    }
     ],
     "faceSize": 2000,
     "initialViewParameters": {
    "yaw": -3.0859786632885857,
    "pitch": 0.058860826755053,
    "fov": 1.5707963267948966
     },
     "linkHotspots": [
    {
      "yaw": 0.007751782217697567,
      "pitch": 0.39202518148107757,
      "rotation": 0,
      "target": "'i-1'"
    },
    {
      "yaw": -3.1285088198075375,
      "pitch": 0.48530966110218543,
      "rotation": 0,
      "target": "'i+1'"
    }
     ],
     "infoHotspots": []
   },<br>
   ')
  }
 
  }
   
  
 }
}

auto(13);
<html>
 <head>
 </head>
 <body>
  <script src="auto.js"></script> 
  
 </body>
</html>
Thriskel
  • 301
  • 2
  • 12
  • One error I see immediately is that JavaScript doesn't allow for multiple line strings unless you use backticks – Austin Ezell Nov 21 '16 at 21:34
  • Also, make sure you increment `i` inside you `while` or you will get an infinite loop. – Damian Nov 21 '16 at 21:46
  • So I tried this: https://gist.github.com/thriskel/3209b4d969131451ee5e5184775a0ad9 but it gave an error "Uncaught SyntaxError: missing ) after argument list" after " document.write(` " – Thriskel Nov 22 '16 at 13:10

1 Answers1

0

First of all have to say something:

  1. Don't use document.write() in loops. It'll rewrite your document itself.
  2. Use backticks(`) to wrap string when it is multi-line string.
  3. It's better to use pre tag to show formatted data like JSON or any code snippets.
  4. While using while loop take care of incrementer and decrementer in your while block to prevent infinite looping.

I think the below solution works for you.

function auto(number){
    var e = document.getElementById("data");
 i = 0;
    var html = "";
 while (i <= number) {
  //Fist Scene
  if(i === 0){
   html += `
   <pre>    
    {
      "id": "` + i + `",
      "name": "0",
      "levels": [
     {
       "tileSize": 256,
       "size": 256,
       "fallbackOnly": true
     },
     {
       "tileSize": 512,
       "size": 512
     },
     {
       "tileSize": 512,
       "size": 1024
     },
     {
       "tileSize": 512,
       "size": 2048
     }
      ],
      "faceSize": 2000,
      "initialViewParameters": {
     "yaw": -3.0907815953112916,
     "pitch": 0.06648956035942888,
     "fov": 1.5707963267948966
      },
      "linkHotspots": [
     {
       "yaw": -3.128953846954726,
       "pitch": 0.47317799909128944,
       "rotation": 0,
       "target": "1"    
     }
      ],
      "infoHotspots": []
    },</pre>
    `;
  }
  
  //Last Scene
  else if (i === number){
   html += `
   <pre>
   {
     "id": "` + i + `",
     "name": "` + i + `",
     "levels": [
    {
      "tileSize": 256,
      "size": 256,
      "fallbackOnly": true
    },
    {
      "tileSize": 512,
      "size": 512
    },
    {
      "tileSize": 512,
      "size": 1024
    },
    {
      "tileSize": 512,
      "size": 2048
    }
     ],
     "faceSize": 2000,
     "initialViewParameters": {
    "yaw": -3.1332154632455715,
    "pitch": 0.062442602034723294,
    "fov": 1.5707963267948966
     },
     "linkHotspots": [
    {
      "yaw": 0.008275683165861025,
      "pitch": 0.3876084470351344,
      "rotation": 0,
      "target": "` + (i-1) + `"
    }
     ],
     "infoHotspots": []
   }</pre>
   `;
  }
  
  //Actual loop
  else if (i < number){
   html += `
   <pre>{
     "id": "` + i + `",
     "name": "` + i + `",
     "levels": [
    {
      "tileSize": 256,
      "size": 256,
      "fallbackOnly": true
    },
    {
      "tileSize": 512,
      "size": 512
    },
    {
      "tileSize": 512,
      "size": 1024
    },
    {
      "tileSize": 512,
      "size": 2048
    }
     ],
     "faceSize": 2000,
     "initialViewParameters": {
    "yaw": -3.0859786632885857,
    "pitch": 0.058860826755053,
    "fov": 1.5707963267948966
     },
     "linkHotspots": [
    {
      "yaw": 0.007751782217697567,
      "pitch": 0.39202518148107757,
      "rotation": 0,
      "target": "` + (i-1) + `"
    },
    {
      "yaw": -3.1285088198075375,
      "pitch": 0.48530966110218543,
      "rotation":0,
      "target": "` + (i+1) + `"
    }
     ],
     "infoHotspots": []
   }</pre>
   `;
  }
 i++;
  }
  e.innerHTML = html;
}

auto(13);
<html>
 <head>
 </head>
 <body>
  <script src="auto.js"></script> 
  <div id="data"><div>
 </body>
</html>
Jyothi Babu Araja
  • 9,598
  • 3
  • 29
  • 38