Currently, I have a requirement to continuously run a loop in the background of my JavaScript-based app. This loop is responsible for cycling a ScrollableView every six seconds. However, the issue arises when this loop prevents any other operations from being performed within the app.
In essence, I am seeking a solution that allows me to run this loop indefinitely while still maintaining the functionality of the app. Any ideas on how to achieve this?
Below is the provided code snippet:
function startScrolling() {
for(; ; ) {
sleep(6000);
Ti.API.info('Scrolling To Index: ' + viewIndex);
scrollView.scrollToView(viewIndex);
if(viewIndex == 4) {
viewIndex = 0;
scrollView.scrollToView(viewIndex);
} else {
scrollView.scrollToView(viewIndex);
viewIndex++;
}
}
}
function sleep(milliseconds) {
var start = new Date().getTime();
while((new Date().getTime() - start) < milliseconds) {
// Do nothing
}
}
UPDATE: Resolved Issue
setInterval(function() {
Ti.API.info('Scrolling To Index: ' + viewIndex);
scrollView.scrollToView(viewIndex);
if(viewIndex == 4) {
viewIndex = 0;
scrollView.scrollToView(viewIndex);
} else {
scrollView.scrollToView(viewIndex);
viewIndex++;
}
}, 6000);