2

At one line of my code I have something like that:

...
while(!$scope.isMyTurn(privilege)){}
I will run next line of codes

However as usual it blocks my browser. I have to wait until get true to run next line of codes. I don't want to block my browser and other functionalities should work because(some of other fired events will change the situation of my turn) User should continue whatever he does.

This is for refreshing a graphic until my turn is come.

2
  • did any of the answers help? Commented Feb 16, 2013 at 17:55
  • @ShaiRez I have solved problem with classical Javascript way but I am looking to implement it with $watch Commented Feb 18, 2013 at 6:42

3 Answers 3

7

You should be doing it something like :

$scope.$watch("isMyTurn(privilege)",function(val){
    if(val){
       //I will run next line of codes
    }
});

instead of what you are attempting to do.. Note that in this scenario, privilege should also be a scope variable something like

$scope.privilege

If you dont want it to be a scope variable just dont pass it as an argument. but directly access it inside the function..

Hope this helps..

Sign up to request clarification or add additional context in comments.

5 Comments

I have solved it via classical Javascript way but it is better to do it with Angular Js built in functionalities. Can you explain that val variable and how can I change the frequency to check next watch time?
privilege is not a scope variable bu it is a defined variable however I get undefined for it?
the proper syntax for scope.$watch is $scope.$watch("isMyTurn(privelege)",function(newVal,oldVal){ if(newVal !== undefined ){ //do your stuff here... } }); I believe you already know that angular works with dirty checking.. So when the value of the watch expression changes, angular calls that call back function providing you the new value and the old value of that expression.. How many times this function is called is not guaranteed, so you should not rely on it..
I use $scope.$watch and doesn't work? I can not directly use privilege variable because the function will use it at another js file?
can you create a fiddle recreating your problem? Then it would be much easier to solve it and show you the solved problem..
5

Consider using the $scope.$watch function.

Look under ($watch): http://docs.angularjs.org/api/ng.$rootScope.Scope

Comments

1

I don't know about angularJS but in normal JavaScript you could use setTimeout

function whenReady()
{
  if ($scope.isMyTurn(privilege)){
    I will run next line of codes
  }
  else
  {
    window.setTimeout(whenReady, 1000);
  }
}
window.setTimeout(whenReady, 1000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.