-1

function checkPrice(afford){
 var z = document.getElementById('man_cost').innerHTML;
 var a = document.getElementById('mouse_cost').innerHTML;
 var b = document.getElementById('farm_cost').innerHTML;
 var c = document.getElementById('factory_cost').innerHTML;
 a = parseInt(a);
 b = parseInt(b);
 c = parseInt(c);
 z = parseInt(z);
 if(afford == 'cannot'){
  if(amount < z){
   document.getElementById('upClick').style.opacity = '.5';
  };
  if(amount < a){
   document.getElementById('upMouse').style.opacity = '.5';
  };
  if(amount < b){
   document.getElementById('upFarm').style.opacity = '.5';
  };
  if(amount < c){
   document.getElementById('upFactory').style.opacity = '.5';
  };
 }else if(afford == 'can'){
  if(amount >= z){
   document.getElementById('upClick').style.opacity = '1';
  };
  if(amount >= a){
   document.getElementById('upMouse').style.opacity = '1';
  };
  if(amount >= b){
   document.getElementById('upFarm').style.opacity = '1';
  };
  if(amount >= c){
   document.getElementById('upFactory').style.opacity = '1';
  };
 };
 return;
};

setInterval(checkPrice('can'),1000);

The setInterval does not seem to be functioning. The price updates every second, but if the price reaches an affordable amount, the checkPrice function does not execute the function and the effects do not take place as they should. However, when i call the setPrice after user interaction, it works.

James Crovo
  • 121
  • 5

3 Answers3

1

You will need to write it as:

setInterval(function(){checkPrice('can')},1000);

This is because the first parameter takes a function, having functionName(..) will give the return value rather than the function itself. Note if the function has no parameters it can be written as: setInterval(checkPrice,1000);.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • It still does not work. could you check my webpage? http://ac.net.co/projects/clicker/clicker.html – James Crovo Dec 16 '15 at 02:54
  • @JamesCrovo Then the problem is not with `setInterval()`, could you make a minimal example of the problem with a fiddle or snippet? I cannot edit the code on your webpage, nor do I even know what is supposed to be happening for it to work. – Spencer Wieczorek Dec 16 '15 at 02:57
  • https://jsfiddle.net/3mvL4kmb/8/ but in the process of copying and pasting, it doesn't really work. – James Crovo Dec 16 '15 at 03:31
0

Your var amount at all if() it has not been defined, this may be the cause it does not work.

Radames E. Hernandez
  • 4,235
  • 27
  • 37
0

If you want pass a value "on the fly" in the execution, try tu use bind()method.

function checkPrice(){
    var afford = this.option;

    var z = document.getElementById('man_cost').innerHTML;
    var a = document.getElementById('mouse_cost').innerHTML;
    var b = document.getElementById('farm_cost').innerHTML;
    var c = document.getElementById('factory_cost').innerHTML;
    a = parseInt(a);
    b = parseInt(b);
    c = parseInt(c);
    z = parseInt(z);
    if(afford == 'cannot'){
        if(amount < z){
            document.getElementById('upClick').style.opacity = '.5';
        };
        if(amount < a){
            document.getElementById('upMouse').style.opacity = '.5';
        };
        if(amount < b){
            document.getElementById('upFarm').style.opacity = '.5';
        };
        if(amount < c){
            document.getElementById('upFactory').style.opacity = '.5';
        };
    }else if(afford == 'can'){
        if(amount >= z){
            document.getElementById('upClick').style.opacity = '1';
        };
        if(amount >= a){
            document.getElementById('upMouse').style.opacity = '1';
        };
        if(amount >= b){
            document.getElementById('upFarm').style.opacity = '1';
        };
        if(amount >= c){
            document.getElementById('upFactory').style.opacity = '1';
        };
    };
    return;
};

setInterval(checkPrice.bind({option: 'afford'}),1000);