0

I have some javascript code that doesnt seem to be automatically updating on the page, any ideas? Have i setInterval correctly?

function updatePrice(id, currentPrice){

    var newPrice = 0;
    currentPrice = currentPrice * 100
    if(rate == 1){
        newPrice = currentPrice - 1;

    }
    if(rate == 2){

        newPrice = currentPrice - 2;
    }
    if(rate == 3){

        newPrice = currentPrice - 3;
    }
    document.getElementById(id).innerHTML = newPrice;
}

updatePrice('reverse', currentPrice);
var timeinterval = setInterval(updatePrice, 60000);

EDIT Thanks for everyones help, the only problem i have with passing arguments via updatePrice is that i want the price to decrease every minute, how would i set the argument to include the new price that has been calculated in the updatePrice function?

For example i think i would need something like: setInterval( function() { updatePrice('reserve',newPrice); }, 60000);

The newPrice being the price just calculated in updatePrice.

Hopefully this makes sense.

4
  • 3
    Your interval is calling updatePrice but not passing any arguments... Commented Apr 5, 2017 at 12:36
  • In your setInterval, you are calling updatePrice with no arguments. Commented Apr 5, 2017 at 12:36
  • The error console may provide illumination. currentPrice seems to be a global and an argument name, the latter shadows the former. Commented Apr 5, 2017 at 12:38
  • w3schools.com/jsref/met_win_setinterval.asp Commented Apr 5, 2017 at 12:38

2 Answers 2

3

Try this setInterval( function() { updatePrice(10,3); }, 60000);

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

Comments

0
function updatePrice(id, currentPrice){
    var newPrice = 0;
    currentPrice = currentPrice * 100
    if(0 < rate && rate < 4) {           // shorten this (it's clearer)
        newPrice = currentPrice - rate;
    }
    document.getElementById(id).innerHTML = newPrice;
}

updatePrice('reverse', currentPrice); // calling updatePrice with parameters (OK)
var timeinterval = setInterval(function() [
    updatePrice(/* SOME PARAMETERS ARE NEEDED HERE */); // should pass in some params
}, 60000);

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.