Making a function affect other function

342
November 26, 2016, at 12:11 PM

I just want to know how to access a function with another function. So if I have the code below, the change function does not affect the loader function. What am I doing wrong ?

var loader = new THREE.JSONLoader();
loader.load( "test.js", function(geometry, materials){
var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
scene.add(mesh);
});
  $('input').change(function() {
    if($("#radio").is(":checked")){
    mesh.material.materials[ 0 ].color.set( 0xff0000 );
    mesh.material.materials[ 1 ].color.set( 0x111111 );
  }
  });
Answer 1

Local variables of a function can not be accessed from other functions. Because javascript functions are closures, you can have a variable shared by both functions:

var loader = new THREE.JSONLoader();
var mesh;
loader.load( "test.js", function(geometry, materials){
  mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
  scene.add(mesh);
});
$('input').change(function() {
  if($("#radio").is(":checked")){
    mesh.material.materials[ 0 ].color.set( 0xff0000 );
    mesh.material.materials[ 1 ].color.set( 0x111111 );
  }
});
Rent Charter Buses Company
READ ALSO
How to get <script src= output to a variable [closed]

How to get <script src= output to a variable [closed]

In Javascript how to get the output of a ?

335
Upload multiple images or pdf files

Upload multiple images or pdf files

I have to upload multiple images at one time from a form when onsubmit event occursHow Do i have to do this one

333
Bind jquery-Impromptu to asp.net MVC6

Bind jquery-Impromptu to asp.net MVC6

I am trying to bind jQuery-Impromptu to an ASPNet MVC6 project in VS2015

320
Using a loop to replace values found in one array with the same index value of a second array?

Using a loop to replace values found in one array with the same index value of a second array?

I'm trying to replace links from a dynamic feed after the results are displayed on the page using JQuery

293