Looping function works 20 times in javascript, but then returns NaN
run = 0
function getLowestPrice(id){
$.get('/items/' + id + '/privatesaleslist', function(data){
var html = $(data);
var lowestPrice = parseInt($('.currency-robux', html).eq(0).text().replace(',', ''));
console.log(lowestPrice);
})
}
while (run < 100)
{
getLowestPrice(362081769);
run = run + 1
}
Webpage I am using this code on: https://m.roblox.com/items/362081769
What happens is when I run this code it runs successfully 20 times returning 3997, and then I get spammed with "NaN" Image.
What i'm trying to get this program to do is check the price of the item for as long as I want, and report it in the chrome console. Why does the function work 20 times and then just return NaN?
I am a beginner at javascript, so please explain your solutions so a beginner like me can understand.
Thanks :)
Answers 1
Hey man I think part of your issue is in the while loop mechanic. Whenever run hits 100 your function will stop being called.
You could use a setInterval function. How this work is it will call whichever function you pass it, every "x" milliseconds.
setInterval will call getLowestPrice with the id of 362081769, every 5 seconds.
Also here is a link to the formal setInterval documentation. https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
I hope this helps :)