Mastering JavaScript Loops

Looking to create an endless loop of links that never stop? I need assistance achieving this.

 setTimeout(linkToGoogle, 5000);
function linkToGoogle() { 
 document.getElementById('myAnchor').innerHTML="Visit Google"; 
 document.getElementById('myAnchor').href="http://www.google.com";

 setTimeout(linkToStackOverflow, 5000);
} 
function linkToStackOverflow() {                                                                         
 document.getElementById('myAnchor').innerHTML="Visit Me";  
 document.getElementById('myAnchor').href="http://www.stackoverflow.com";

 setTimeout(noLink, 5000); 
} 
function noLink() {  
 document.getElementById('myAnchor').innerHTML="Visit None"; 
 document.getElementById('myAnchor').href="";
}

Answer №1

//Information
var sites = [
    {linkName: "Explore Facebook", siteURL: "http://www/facebook.com"},
    {linkName: "Explore Twitter", siteURL: "https://www.twitter.com"}
], x = 0;

//Functionality
function browse(){
    var chosenSite = sites[x++%sites.length];
    document.getElementById('myLink').innerHTML = chosenSite.linkName;
    document.getElementById('myLink').href = chosenSite.siteURL;
    setTimeout(browse, 6000);
}

Preview: http://jsfiddle.net/SarahP/JRdht/

Remembering my earlier statement: Avoid duplicating your code. Let the machine handle it for you. With this approach, resolving issues will become simpler in the long run.

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

`How can you generate navigation paths using the ngRoute module?`

I am currently working on creating a basic navigation system like the one illustrated below: html: <html ng-app="myapp"> <body> <ul> <li><a href="pages/sub1">sub1</a></li> <li><a href="pages/ ...

"Utilize ajax and a modal window to insert a new record

Unable to retrieve values from ajax request when trying to add records using modal. (home.php) <script type="text/javascript> function saveData(){ var modsubj = $('#modalsubject').val(); var modsect = $('#modalse ...

The challenge of maintaining coherence in AngularJS scopes

It's driving me crazy. Our integration with some ATSs involves sending queries and setting variables in the scope upon receiving responses. I always make sure to set the variables within a $scope.$apply() to ensure proper updating. Everything was work ...

Using React links in combination with dangerouslySetInnerHTML to render content is a powerful and flexible technique

I am in the process of creating a blog page for my react application. The content on this page is retrieved from a CMS and contains raw HTML code that I display by using: <div dangerouslySetInnerHTML={{__html: this.state.content}} /> However, when I ...

The CORS policy is preventing the AJAX request from functioning properly on localhost

Recently, I have been working on an Angular application that requires interaction with an API. To test it out, I set up an API on my localhost and made a request using AJAX. However, I encountered the dreaded CORS error. Despite trying various solutions fo ...

Ways to implement a parallax effect by changing images within a specific div on scrolling with the mouse

First and foremost, thank you for taking the time to review my question. I am currently developing a website and I need to implement a vertical image transition effect within a selected division, similar to a parallax effect. Within this div, there are 3 ...

Expanding the sidebar panel during a redirection

I am facing an issue with the expansion panels in my sidenav. I have successfully been able to track and set their open state as they are opened and closed. However, when a list item within the panel is clicked and redirects to another route, the panel c ...

The <mat-radio-button> component does not have a value accessor specified

When working with HTML and Angular, I encountered the following issue: <mat-radio-group> <mat-radio-button [(ngModel)]="searchType"> And (Narrower search) </mat-radio-button> <mat-radio-button [(ngModel)]="searchType"&g ...

What is the best way to distinguish between inline javascript and dynamically created content in Express/Node.js?

I've encountered a bit of a noob-question despite having a few years of experience in web development. Despite searching Programmer Stack Exchange and Google, I haven't found an answer, so here goes. Currently, I'm working with the Express ...

I'm experiencing difficulty accessing the correct identification number for my questions on the website

Hi, I'm currently developing a website using Meteor where users can post questions and receive answers. I want to implement a feature that allows users to delete their own questions. When I try to directly pull the ID of the question and delete it, it ...

Navigating from production lines to the angular directive - what's the best route?

Can anyone advise on the proper method to initialize a variable with factory data in Angular? The current approach I'm using doesn't seem right to me. Am I doing it wrong? $scope.result = []; factoryCities.then(function(data){ $scope.result ...

Encountering the error code [$injector:unpr] regarding an unknown provider: $uibModalInstanceProvider

In my web application, I have a controller called AddPrice. This controller is invoked from a Price listing screen as a popup using the $uibModal.open method, where the controller is passed in as an argument. However, the controller is not defined within t ...

Create fluidly changing pictures within varying div elements

Hello there! I have a form consisting of four divs, each representing a full page to be printed like the one shown here: https://i.sstatic.net/w7N6E.png I've successfully created all the controls using AJAX without any issues. Then, I load the image ...

The behavior of AngularJS checkbox and ng-change is definitely puzzling

I am facing an issue with my array structure: { key : 'abc', color: 'Red', show: true } <div ng-repeat="x in myArray"> <input type="checkbox" ng-model="x" ng-change="changeShow($index)" checked="checked" /> ...

Is there a way to automatically populate an AngularJS input field?

Attempting to automate filling out a website using JavaScript. The script below: document.getElementsByClassName('form-control')[1].value = "MYNAME"; The input text changes, but upon clicking the submit button it displays as empty. Any as ...

The requested Javascript function could not be found

I have the following JavaScript function that creates a button element with a click event attached to it. function Button(id, url, blockMsg){ var id = id; var url = url; var blockMsg = blockMsg; var message; this.getId = function(){ return id; }; th ...

Triggering a dynamically created event with the onchange event

I'm currently working on creating an input type file, and here is the code I have: var element = document.createElement("INPUT"); element.type = "file"; element.id = "element" + i; $("#media").append("<br>"); $("#media").append("<br>"); $ ...

Getter and Setter Implementation in Typescript without Using Classes

Check out these various SO questions discussing Typescript getters/setters: from 2015, Jan 2018, Sept 2018, and more. Now, the question arises - what is the best approach to define Typescript types for getters/setters in a plain JavaScript object without ...

What is the best way to switch out the characters 'a' and 'b' in a given string?

Let's say we have the following text. Lorem ipsum dolor sit amet The revised text should look like this: merol merol merol merol Is there a predefined function in JavaScript that can help us replace characters like this within a string? ...

When using Google Maps Autocomplete, always make sure to input the full state name instead of just the state

Utilizing the Google Maps autocomplete API, we have enabled our customers to search for locations in the format of city, state, country. The functionality is effective overall, but a recurring issue arises when searching for cities, such as 'Toronto&a ...