In my app, I connect to a news service and pull back an array of articles; I do this every 20 minutes.
During that 20 minutes, I loop through the array of articles I have pulled, a new article from the array every 15 seconds.
My problem is that after 20 minutes, I get a new array from the news service and start looping through that array. Apparently, I am doing a double loop, the original array plus the new array.
Then, after another 20 minutes, I am looping through yet another array of news articles ... three in total now.
This continues throughout the day until it turns into an ugly mess.
I'm trying to figure out how to stop the original loop once I get a new array:
clearInterval(articleIntervalVar);
newsArticlesArray = null;
But nothing I have tried works, and I cannot figure it out.
newsFunction();
newsIntervalVar = setInterval(newsFunction, 1200000);
function newsFunction() {
xmlHttpNewsObject = GetXmlHttpObject();
if (xmlHttpNewsObject === null){
alert ("Browser does not support HTTP Request");
return;
}
var url = "";
xmlHttpNewsObject.open("GET", url, true);
xmlHttpNewsObject.onreadystatechange = getTheNews;
xmlHttpNewsObject.send(null);
}
function getTheNews(){
if (xmlHttpNewsObject.readyState == 4 && xmlHttpNewsObject.status == 200){
var newsArticlesArray = JSON.parse(xmlHttpNewsObject.responseText).articles
drawTheNews(newsArticlesArray);
return;
}
}
function drawTheNews(newsArticlesArray) {
var news = document.getElementById('news');
var numberOfArticles = newsArticlesArray.length;
var i = 0;
news.innerHTML = newsArticlesArray[i].title);
var articleIntervalVar = setInterval(function () {
i++;
if (i === numberOfArticles) i = 0;
news.innerHTML = newsArticlesArray[i].title);
}, 15000);
}