Edit script: Iterate through the different div elements at regular intervals AND pick a specific div by pressing a button

Check out this interesting discussion about displaying and hiding divs at specific time intervals using jQuery on the page here.

The script mentioned is designed to loop through DIVs, showing one after the other while hiding the rest. It's titled "Loop through divs every 10 seconds."

Although this script functions well, there is a need to select a specific DIV by clicking on a designated button as well.

Within the context of three DIVs labeled 1-2-3 (with the active one in red), the Loop Script alternates between them correctly. However, when clicking on 2, DIV Nr. 2 should become the active one (then continue with 3 - 1 - 2...).

To simplify: How can I indicate to the script to set "counter = 3" upon clicking button 3?

For more insight and examples, visit these links: timer-based script and click-triggered version.

If you can explain it to someone who is not very familiar with JavaScript (like me!), that would be greatly appreciated!

Thank you!

Answer №1

"Essentially, how can I specify that the script sets "counter = 3" when button 3 is clicked?" -- I'm assuming you are referring to the script mentioned in this source

If you have a button like this (for example):

<input type="button" id="button-3" value="Click me">

The JavaScript code for handling the click event would be as follows:

$("#button-3").click(function() {
    counter = 3;
});

This click handler needs to be within the same scope as the counter variable to function correctly (in the case of the linked script, it can be placed right after the declaration of var counter = 0;);

Additional Note: To ensure that the div switching occurs at the desired time intervals, you should implement intervalId management.

HTML:

<a href="#" class="button" rel="1"><img width="11" height="10" alt="video 1" src="orange.gif"></a>
<a href="#" class="button" rel="2"><img width="11" height="10" alt="video 1" src="grau.gif"></a>
<a href="#" class="button" rel="3"><img width="11" height="10" alt="video 1" src="grau.gif"></a>

JavaScript:

$(".button").click(function() {
    counter = parseInt(this.rel);
    showDiv(); // display next div
    setIntervalSwitch(); // reset the interval for the next "switch" after X seconds
}); 

var intervalId;
var setIntervalSwitch = function() {
    clearInterval(intervalId); // clear previous interval
    // start new interval
    intervalId = setInterval(function () {
         showDiv(); // display next div
    }, 5 * 1000);
}
// call setIntervalSwitch to initialize interval before someone manually clicks on any div
setIntervalSwitch();

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

Tips for moving the title to the next line within a card design

I'm currently working on an antd Card and I am trying to figure out how to display the title on two lines if it is too long. For example, instead of showing "This title needs to go to ne...", I would like it to be split into two lines as "This title n ...

What is the technique for enabling a mouse click in the vicinity of an element?

During a presentation on React at a conference, I witnessed the speaker introduce a feature that allowed users to click and drag a divider on the screen by simply getting close to it instead of having to click directly on it. This concept adds to usabilit ...

Why is the count's answer 88?

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> </head> <body> <div ng-app="myApp" ng-controller="personCtrl">{{count ...

Tips for aligning text and image in the center of a div with a specific width:

I'm having a hard time figuring this out for some reason. I need to center both text and an image on the same line within a div that has a 100% width. Check out this jsfiddle link for reference: http://jsfiddle.net/JnbeJ/ ...

Compose an abbreviated version of the function

I am facing an issue with a function that involves calling out specific states with similar lines of code. Below is the function in question: DoThis(type) { if (type === 'a') { this.setState({ activeA: { display: 'block', opac ...

Using Angular's $filter to target a specific field

I'm attempting to use the $filter function in my controller, within a $watch function, to filter specific fields. I am having trouble understanding the documentation provided. The situation: I have a dropdown menu that serves as the filter parameter. ...

Tips for integrating bootstrap code within PHP tags

<?php $con=mysqli_connect("localhost","root","","testdatabase"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_qu ...

What could be the reason for meteor not injecting the text from my template helpers?

My goal is to dynamically generate a table displaying two different sets of data. Despite having verified returns from my database, the rendered page does not show the corresponding HTML elements as expected. Here is the template and HTML code: <templ ...

What is the best way to maintain data types of variables within an object sent via ajax?

I'm sending an object via ajax to a PHP script for processing. Here's how I'm doing it: var Obj = {id:1, name:"John", value:12.1}; $.ajax({ url : "myfile.php", type : 'POST', data : Obj, success : ...

Tips for activating swf file autoplay on the mobile edition of WordPress

I recently added a swf file to my WordPress page (https://www.pacifictintlv.com/mobile). However, I have noticed that it does not seem to work properly when viewed on mobile devices. Is there a way to redirect users to the Flash download link if they do ...

Issue with setting HTML content using jQuery's .html() method

I have a simple functionality. When a user clicks on the edit link, it transforms the previous element into an input element for editing, and changes the edit link into a cancel link. If the user decides not to edit, they can click on cancel and everything ...

Accessing JSON Data Using JQUERY

Currently, I am experimenting with grabbing JSON data from websites using the $.getJSON() method. Here is the code snippet I have been working on: The website I am attempting to retrieve JSON data from can be found here. Interestingly, the code functions ...

How to reference an object from an external file in TypeScript using Ionic 2 and Angular 2

I am currently developing a mobile application with Ionic2 and have integrated a simple online payment service called Paystack for processing payments. The way it operates is by adding a js file to your webpage and then invoking a function. <script> ...

When using an HTML Select element with options loaded through Ajax, the desired item for selection is not being automatically selected

This is a strange situation. Collaborating with others and using sample code, I have created a routine that utilizes AJAX to pass a value from one select to a PHP file. The PHP file then generates a second select with options, each option designed to check ...

It is not possible to invoke the Ajax function on the subsequent page, while using AJAX and PHP

My current ajax function has a total of 7 parameters, but the focus today is on the fifth parameter known as "stop". When this parameter is set to "true", the ajax function halts the setTimeout feature and essentially stops calling itself unless triggered ...

Trouble with Angular 1.6 ng-repeat not showing search results

I can't seem to figure out why ng-repeat is not showing the search results. Here's what works: Using an HTTP GET request to retrieve all data from the database, and seeing the results displayed by ng-repeat. Sending an HTTP GET request with ...

Font Awesome functions properly on one Angular component but not on the second one

I've encountered a strange issue while working on my Angular project. About a month ago, I added a component with a form that included some inputs along with Font Awesome icons. The code snippet is provided below. <div class="field"> ...

Scanner (IE5) impact on page load speeds

I have developed an MVC project which is designed as a single-page application (SPA) utilizing knockoutjs and sammy frameworks. The following script is placed in the head section of the page: <script> var startTime = (new Date()).getTime(); </ ...

Bootstrap is not functioning successfully

<!DOCTYPE html> <html> <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0c01011a1d1a1c0f1e2e5b405f405f">[email protected]</a>/d ...

Using JavaScript/jQuery to analyze POST values just before submission

Currently facing a dilemma as my company is in the process of redesigning its website. I find myself stuck trying to come up with a temporary fix for an issue on the existing site. The solution requires the use of JavaScript or jQuery. The problem arises ...