Execute a Loop in the Background

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);

Answer №1

Check out window.setInterval() for more information.

/* 
    This function will repeatedly call another function
    with a specified time delay between each call.
*/
setInterval(startScrolling, 6000);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Connecting MVC Model data with AngularJS ModelHere are some strategies for linking data between

Currently, I am utilizing MVC Razor View in combination with AngularJS. Within the controller, I am initializing the UserMaster object and passing it along to the view. Function AddNewUser() As ActionResult Dim objUser As New UserMaster() ...

The method of reading a unique array of objects for each radio button

Currently, I am facing an issue when trying to retrieve unique elements for each radio button from the database. The data structure and information obtained from the database are as follows: { FormularID: 182, CampaignID: 14, FormLabel: & ...

An error in TypeScript has occurred, stating that the type 'IterableIterator<any>' is neither an array type nor a string type

Hey there! I'm currently working on an Angular project and encountering an error in my VS Code. I'm a bit stuck on how to fix it. Type 'IterableIterator<any>' is not an array type or a string type. Use compiler option '- ...

Error in Express.js application: form fields lose their values

I am currently developing a blogging application using Express, EJS and MongoDB. One of the main features is an "Add New Post" form with an addPost() method in the controller; exports.addPost = (req, res, next) => { const errors = validationResult ...

What is a reliable method to retrieve the text from the current LI if all LI elements in the list share the

I'm encountering an issue with retrieving the text from LI elements because I have 10 list items and they all have the same class name. When I attempt to fetch the text using the class name or id, I only get the text of the last item. Here is my code ...

Why does the AJAX request only execute successfully once on my webpage?

Currently, I am working on developing an online tic-tac-toe game. In the JavaScript function that I have created, I check if a particular area of the screen has been clicked and is not already marked with an X or O. If it is unmarked, I proceed to mark it ...

Tips for formatting the body of a post request

After sending a data from a nodejs server Using POST REQUEST { "id": "285104348274884628", "username": "TEST USERNAME", "apiKey": "5WA8G5LUYPJB8RII64RE443EFTTY-PY" } The Post Code In ...

Error: The 'current' property is not defined and cannot be focused on

I have a total of 4 input fields, and I would like the user to automatically move to the next input field after filling out the current one. constructor(props) { ... this.textInput1 = React.createRef(); this.textInput2 = React.createRef(); ...

Combine and interchange horizontal and vertical form designs within antd 5 for a personalized touch

I need to customize the layout of a form to display checkboxes in vertical mode. Specifically, there is a row of checkboxes that I want to appear vertically aligned rather than horizontally. In the past, I was able to achieve this in antd 4 by applying a ...

"Enhance your website with dynamic PHP Ajax live search and infinite scrolling

When scrolling to the bottom of the .dropdown-menu, I would like to load an additional 7 rows from the database. However, I haven't been successful in implementing this feature using the script provided. I am currently utilizing Bootstrap CSS and JS f ...

Displaying JSON data on an HTML page

i am currently developing a web application and i am facing an issue with retrieving data from JSON files and displaying them in table format. The problem arises when i encounter a 404 error, as i am working locally using Node.js. Edit 1: upon checking ...

Choosing an automatically generated choice from a C# web browser control

Using a c# web browser control, I am navigating a commercial website to access a list of potential jobs. However, I encountered an issue while trying to bid on these jobs through some links. The problem arises when dealing with forms that contain two sele ...

Issue with decodeURI function causing hyperlinks to display as plain text

I have been developing a Sharepoint App that includes a feature to extract contact details from a list on the Sharepoint site. Below is a snippet of my code: var currentOpeningContent = '<h4 onclick="ShowJobDetail(\'' + encodeURI(cu ...

Utilize features from both angular-strap and ui-bootstrap to enhance your project

Is it possible to efficiently utilize components from both angular-strap and ui-bootstrap without encountering conflicts that could potentially disrupt the application's functionality? For instance: Opt for a customized version of ui-bootstrap that ...

The server is not being reached by the Ajax PUT method

Having some issues with my login form when attempting to use the PUT method for logging in and storing the username. It seems like it's not communicating with the server properly and I'm unable to log in. Any help would be greatly appreciated. I ...

Material UI Date-Picker: Placeholder for Month Abbreviation with 3 Letters set as "MMMM" instead of the usual "MMM"

I'm currently using the most recent version of @mui/x-date-pickers (6.16.0). In my code snippet below, I have set the format of the text field input to be in "MMM DD, YYYY" style. However, when the date picker field is empty, the placeholder text disp ...

Creating an angular controller in a template based on certain conditions

When working with Angular.js, I'm attempting the following: index.html <div class="data-tabs-sms-scroll" ng-show="question.type == 'open'" ng-controller="AudioMessagesCtrl" ng-include="'/templates/audioMessages.html' ...

Rails database is not properly embedding into Javascript when using as_json.to_json method. (results in "&quot" characters)

I have scoured nearly every SO past question without success. Within my main.html.erb file, I am mixing html/erb/javascript. (I understand it's not ideal, but I'm still grappling with asset pipelines and this is just a small project.) My goal i ...

The statement ""x=x || 4" will result in a `ReferenceError: x is not defined` because the

What is the reason behind receiving a ReferenceError: x is not defined error when using x = x || 4 or even x=(x||5), while var x = x || 4 operates as intended? ...

How to retrieve the clicked value using jQuery within a jinja2 loop

I'm currently working on an app that utilizes a Flask backend and Jinja2 templating. Within the HTML, I have a loop set up to organize data into batches, creating three columns on the web page: {% for batch in df.iterrows() | batch(3) %} <d ...