1

I have a method in my java code that is calculating the current price, i then use this in my javascript code shown below.

function updatePrice(id, currentPrice){

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

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

    newPrice = cPrice - 3;
}
document.getElementById(id).innerHTML = newPrice/100;;
return newPrice / 100;
}



var nPrice = updatePrice('reverse', currentPrice); //The new calculated price, currentPrice is the price first injected into the script 
var timeinterval = setInterval(function() { nPrice = updatePrice('reverse', nPrice); }, 60000); //the nPrice as currentPrice

I want to be able to use the newPrice calculated in updatePrice on itself when running setInterval, so that once a minute the newPrice is being used to go down by 1, 2 or 3(pence);

Its probably something quite simple that i just havent noticed yet.

Hopefully this makes sense, fill free to request more info if needed.

1 Answer 1

1

Your initial issue is that you are not setting nPrice equal to the return value of updatePrice('reverse', nPrice). As a result, every iteration will use the same value for nPrice and return the same result.

You can modify the code like so to get it to decrement:

var nPrice = updatePrice('reverse', currentPrice);
var timeinterval = setInterval(function() { nPrice = updatePrice('reverse', nPrice); }, 60000);

However, there are more things you can change to optimize your solution.

function updatePrice(id, currentPrice, rate){
    var newPrice = 0;
    var cPrice = Number(currentPrice) * 100;
    newPrice = ((cPrice - rate) / 100).toFixed(2);
    document.getElementById(id).innerText = newPrice;
    timeout = setTimeout(function () {
        updatePrice(id, newPrice, rate);
    }, 1000);
}

var rate = 1;
var currentPrice = 10;
var timeout = null;
// The new calculated price, currentPrice is the variable 
// first injected into the script from jsp
updatePrice('reverse', currentPrice, rate);
<div id="reverse"></div>

Since your rate coincides with the amount you want to decrease, there is no need to have if statements to handle the different values of rate. You can simply say cPrice - rate.

You can also use a recursive setTimeout rather than a setInterval so that you do not have to update or keep track of any global variables.

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

5 Comments

Thanks for your help @mhodges, youve helped me gain a little more understanding. The problem im faced with is that i need to price to decrease automatically (which is why im using setInterval) however it doesnt seem to be doing this. I have to refresh the page for the value to change. Any ideas.
@HazardAGuess As long as currentPrice and rate are defined, my code would do exactly that. I will create a demo for you and also change up how it is working a little bit to fit some best practices.
@HazardAGuess Updated. Take a look
This is great @mhodges, runs well now, really appreciate your time.
@HazardAGuess Glad I could help =)

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.