cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I update a variable within $timeout?

SOLVED
Highlighted
Gravel

How do I update a variable within $timeout?

I have a function within a $timeout to which I want to pass a variable called 'renderlevel'.

 

Later I want to update 'renderlevel' and for it to update within $timeout. Can someone advise me how to do this? 

 

var renderlevel = 0.2;

$timeout(function() {
for (i = 0; i < rLen; i++) {
$scope.view.wdg[renComponents[i]]['texture'] = "app/resources/Uploaded/MyImage.jpg?name=tex0&edge=repeat";
$scope.view.wdg[renComponents[i]]['shader'] = "reflect;mixer f " + renderlevel;
}
} ,50);

//Later...
renderlevel = 0.5;
//Update timeout??

Update: this is a lot simpler than I realised. This is my basic but functional solution:

 

//SHADER - runs at startup
$timeout(function() {
for (i = 0; i < rLen; i++) {
$scope.view.wdg[renComponents[i]]['texture'] = "app/resources/Uploaded/MyImage.jpg?name=tex0&edge=repeat";
$scope.view.wdg[renComponents[i]]['shader'] = "reflect;mixer f " + renderlevel;
}
} ,50);

//SHADER UPDATE - runs when called
$scope.shaderUpdate = function(){
$timeout(function() {
for (i = 0; i < rLen; i++) {
$scope.view.wdg[renComponents[i]]['texture'] = "app/resources/Uploaded/MyImage.jpg?name=tex0&edge=repeat";
$scope.view.wdg[renComponents[i]]['shader'] = "reflect;mixer f " + renderlevel;
}
} ,50);
}

//SHADER AMOUNT
$scope.shaderLevel = function(){
renderlevel = 0.5;
$scope.shaderUpdate();
}
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: How do I update a variable within $timeout?

Hi @rhudd ,

another possible way to call the update function on change is to use a $watch construct - something like this :

 

$scope.renderLevel=0.2;
...
...
////////////////
// here the definition of shaderUpdate...
$scope.shaderUpdate= function (renderLevel) {
console.log(" called shaderUpdate() with renderLevel =" + renderLevel)
console.log("call here your code using arg renderLevel")
}

/=============================================================
$scope.$watch('renderLevel', function (newValue, oldValue, scope) {
 // console.warn("called watch function with newValue=" +newValue+ " <=> oldValue was="+oldValue+ " <=> scope.renderLevel="+scope.renderLevel);
  //pay attention here is scope without $ !
   if(scope.renderLevel!= undefined) {
    scope.shaderUpdate(scope.renderLevel)
    $scope.$applyAsync();
  }
});

...
...
/// later in code
$scope.renderLevel=0.5;
//will call the update function 

 

This code will call also renderLevel for the 0.2 value on start

 

View solution in original post

2 REPLIES 2
Highlighted

Re: How do I update a variable within $timeout?

Hi @rhudd ,

another possible way to call the update function on change is to use a $watch construct - something like this :

 

$scope.renderLevel=0.2;
...
...
////////////////
// here the definition of shaderUpdate...
$scope.shaderUpdate= function (renderLevel) {
console.log(" called shaderUpdate() with renderLevel =" + renderLevel)
console.log("call here your code using arg renderLevel")
}

/=============================================================
$scope.$watch('renderLevel', function (newValue, oldValue, scope) {
 // console.warn("called watch function with newValue=" +newValue+ " <=> oldValue was="+oldValue+ " <=> scope.renderLevel="+scope.renderLevel);
  //pay attention here is scope without $ !
   if(scope.renderLevel!= undefined) {
    scope.shaderUpdate(scope.renderLevel)
    $scope.$applyAsync();
  }
});

...
...
/// later in code
$scope.renderLevel=0.5;
//will call the update function 

 

This code will call also renderLevel for the 0.2 value on start

 

View solution in original post

Highlighted

Re: How do I update a variable within $timeout?

Hi @RolandRaytchev,

 

Thank you - I've learnt something new! 

 

I'm still quite new to AngularJS so I really appreciate your input 🙂

Announcements
Join an
AR Working Group!

Topics available:
AR/VR for Data Optimization AR/VR for Security and Control AR/VR for Inspection