Switch out a scheduled 'click' event for setInterval with mouseenter and mouseexit

I've been struggling with this task for a while and can't seem to figure it out. I've been experimenting with this JSFiddle but I can't change the trigger from click to hover or mouse enter/exit. I tried implementing suggestions from similar issues, but I could only get the font to change once per mouseover. Am I approaching this correctly, or is there an easier way to achieve what I want?

It seems that the "click" trigger is set every 1.5 seconds, which closes the expanding nav bar on my website whenever it's clicked. My goal is to make the font rotate only when hovering over a specific id or class. I saw other questions mentioning stopping or clearing the interval... Thank you in advance for any help.

HTML

<div id="container">
    <div id="change" class="one"></div>
</div>

CSS

@import url(http://fonts.googleapis.com/css?family=Josefin+Slab|Quattrocento|Sacramento|Give+You+Glory|Poiret+One|Khand|Permanent+Marker|ABeeZee|Ubuntu|Questrial);

#change {
  font-size:2em;
  text-align:center;
  color:#636466;
}
#change:before{
    content:'Change during hover, stop when mouse leaves';
}
.one:before    {
  font-family:'Questrial';
}
.two:before    {

  font-family:'Give You Glory';
}
.three:before  {

  font-family:'Khand';
}
.four:before   {

  font-family:'Josefin Slab';
}
.five:before   {
  font-family:'Sacramento';
}
.six:before    {

  font-family:'ABeeZee';
}
.seven:before  {
  font-family:'Permanent Marker';
}
.eight:before  {
  font-family:'Ubuntu';
}
.nine:before   {
  font-family:'Poiret One';
}

JS

setInterval(function() {

   var e = $.Event("click");

    $('.one, .two, .three, .four, .five, .six, .seven, .eight, .nine').trigger('click');
}, 1500);

$('.one, .two, .three, .four, .five, .six, .seven, .eight, .nine').hover(function() {
    this.className = {
        three : 'four',
        four  : 'five',
        five  : 'six',
        six   : 'seven',
        seven : 'eight',
        eight : 'nine',
        nine  : 'one',
        one   : 'two',
        two   : 'three'
    }[this.className];
});

//clearInterval(function() {?

Answer №1

Make sure to save the reference to the timer returned by setInterval:

var intervalId = setInterval(function() {

   var e = $.Event("click");

    $('.one, .two, .three, .four, .five, .six, .seven, .eight, .nine').trigger('click');
}, 1500);

clearInterval(intervalId):

For more examples and information on how to use this method, check out the MDN documentation.

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

Is your background image not filling the entire page?

Just diving into the world of HTML and CSS (with a touch of Bootstrap), I've set out to create a basic scrolling webpage. However, I've hit a roadblock with the background image not covering the full height of the page. It seems to stop at the h2 ...

Extract the file name from a URL using Vue

I have a URL for a file that I need to display to users after slicing. Initially, I successfully sliced the URL using substring, but now I face an issue with varying numbers in the string to be sliced. For example, if the URL is /media/users/3/sample.doc ...

Turn off the Google default date picker on the MVC Scaffolded view

After scaffolding some view with MVC, I incorporated a jQuery datepicker dropdown that matches the color scheme perfectly. Everything functioned smoothly in Internet Explorer, but upon testing in Chrome, I encountered a second date dropdown overlapping my ...

Content width exceeds body width

Having an issue with body width that seems strange. Check out this fiddle link to see for yourself. <div class="topbar" style="width:100%; background:#000; color:#fff"> <div class="container" style="width:970px; margin:0 auto;">Lorem ipsum ...

Tips for adjusting UI with Media queries based on screen dimensions

Looking for a better solution I currently have 5 sets of items. Button Text Dropdown Pagination Icon For larger screen sizes, specifically @media (min-width: 990px) {} I would like all items to be in a single row, like so: [button][Text][Dropdown][Pagi ...

Creating a dynamic radio button group with buttons

Currently, I am occupied with crafting some helper functions for my colleagues in the office who are not very familiar with Bootstrap. This involves taking basic HTML and transforming it into Bootstrap layouts. Specifically, I am working on creating a jus ...

What is the best way to add HTML tags both before and after a specified keyword within a string using JavaScript?

Imagine I have a string similar to this, and my goal is to add html tags before and after a specific keyword within the string. let name = "George William"; let keyword = "geo"; Once the html tags have been appended, the desired result should look like ...

"The seamless integration of Ubergallery with AJAX functionality creates a

When I attempted to access the Ubergallery through an Ajax call, I encountered numerous issues that I was able to resolve using the script provided below. However, despite resolving most of the problems, I am now facing a new issue - the navigation buttons ...

Ways to retrieve every parent up to a specific parent being reached

I'm looking for a solution that falls between the functions of .closest() and .parents(). My goal is to apply certain CSS styles to all parent elements of a specific child element up to a certain ancestor. Currently, I am using a `while` loop to itera ...

Implementing Object-Oriented Programming (OOP) to insert data into a database in PHP

Seeking a method to insert data into a MySQL database without the need to refresh the page and ensuring no security vulnerabilities. After several hours of experimenting and researching, I have come up with this solution: <form name="form1" action="" ...

When using REACT to fetch data from an API, the absence of an 'Access-Control-Allow-Origin' header may result in access issues

I am working on a project that involves retrieving products from a company's API. After reaching out to the company, they provided me with the following information: CORS allowed origins for local development is "http://localhost:1229" To adhere t ...

The React sidebar expanded to cover the entire screen width

As I work on creating a dashboard page that will display a sidebar after the user logs in, I am facing an issue where the sidebar is taking up the full width of the page, causing my Dashboard component to drop to the bottom. You can view the image link of ...

$resource transformResponse function is malfunctioning

Take a look at this simplified example using $resource (adapted from Angular's website): angular.module('project', ['mongolab']); function ListCtrl($scope, Project) { $scope.projects = Project.test(); } angular.module('mo ...

How to access elements in every document fragment

I am facing an issue with a page that contains multiple document fragments. I need to retrieve all elements from the entire page that match a specific selector, like so. document.querySelectorAll('iframe') However, this does not return elements ...

Firefox not clearing Highcharts points properly

I am currently utilizing highcharts to generate dynamic charts when hovering over a table row. My goal is to clear and hide the chart once the mouse moves away from the row. While this functionality works smoothly in Chrome, I am encountering a strange is ...

Passing Select Box Value as Foreign Key in Django: A Step-by-Step Guide

Hello, I am currently developing a Django web application. Within this app, there is a select box that contains all objects from a specific model. This select box is generated in the template and is not directly part of the form. When a user submits the fo ...

Refresh only a section of a webpage following an asynchronous request

I'm currently working on implementing a search module for my data listing. The function I am posting the request to renders a view. How can I make the list grid load after clicking the search button? Should I use an echo to insert HTML code, or ren ...

What are the steps to create offline documentation for sails.js?

I am looking to integrate offline sails.js documentation into my system. You can find the official documentation for sails.js maintained at this Sails.js Documentation. According to their documentation, they use doc-templater for building the documentati ...

Is it possible to utilize the onError attribute for activating an HTML data-remodal-target?

I've been working on a new website and recently incorporated Remodal into it. When using data-remodal-target to trigger modals, I noticed that if there is an error like a missing image or stylesheet, the site may not function properly. I am looking fo ...

The Strength of jQuery Selectors

As I dive into the world of jQuery, I am struggling to find any resources that directly cover my query. When it comes to performance, is it more efficient to select $('#element') or $('div#element')? Does the syntax make a significant ...