What is the correct way to effectively clear an interval in my particular situation?

Check out the live demo here

    $(".moving_container").mouseenter(
        function(){
             clearInterval(timer);
        }
    ).mouseleave(function(){
             timer = getInterval(slideWidth,slideHeight,slideLength)
    });  

I'm currently working on developing a slider and I want it to pause when the cursor is over the slider and then resume when the cursor moves away. However, the functionality seems to be glitchy.

To replicate the issue: Resize the screen and hover your mouse over the slider Then move the cursor out of the slider

Here's another demo for reference

Any tips on how I can properly clear the interval would be greatly appreciated!

Answer №1

The primary issue with the code lies in the repetitive addition of the mouseenter and mouseleave events whenever the window is resized. I recommend reorganizing your code to ensure that these events are only attached once, thus necessitating a restructuring of your current setup.

An expedient fix would involve detaching the events before reattaching them:

$(".moving_container").off('mouseenter').off('mouseleave');

For a more robust solution, a complete rewrite may be necessary: JSFiddle

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

Running both the React FrontEnd and NodeJs Backend on a single server

I have developed a full-stack application using React for the frontend and server.js written in nodejs(Express), with Postgres as my database backend. I have previously deployed a similar setup on Heroku, where I hosted my site. The difference is that on H ...

Surprising outcomes encountered when playing audio with JavaScript

https://i.sstatic.net/1jz45.png I've been diving into learning JavaScript and decided to create a simple web page. This page, when Pikachu (image) is clicked, plays an audio file. Similarly, if the string "Pikachu" is typed into the form, it should ...

Retrieve data from the scss document

I'm trying to extract color values from an scss file where they are stored as variables like $base-colour:#fff444. My goal is to display these color values in a column of an HTML table. Does anyone have any suggestions on how to achieve this? ...

Creating a customized design for a React application with the use of Scss and JavaScript variables

Is there a method to incorporate JavaScript variables into an SCSS file? For instance, I have a JavaScript file containing the following code: // sky blue const PRIMARY_COLOR = '#489FEF' export { PRIMARY_COLOR} and I would like to utilize it ...

Grouped validation of redux form fields using nesting

Is this usage of redux-form Fields considered valid? const ValidatedFieldGroup = (props) => { const {meta: {touched, error}} = props return ( <div className={touched && error ? 'has-error' : ''}> <Fiel ...

Unlocking the Power of arrayFilters in MongoDB for Efficient Pipeline-Style Updates

Currently utilizing Mongo 4.4, I am attempting to execute an update operation with the aggregation pipeline using updateOne to modify nested array elements. To target a specific array member for updating, I include an arrayFilter in the options. However, e ...

Interactive forms utilizing Ajax, JQuery, and JSON

I'm encountering an issue with posting form information to an external site, as I keep receiving an error message: Error: [Exception... "Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)" location: ...

Tips for implementing autocomplete functionality in AngularJS input fields

I attempted to integrate code from a website (http://jsfiddle.net/sebmade/swfjT/) into my program, but unfortunately, the output did not match what was expected. I am also looking to implement a search function by camera id. Can anyone provide assistance? ...

Design a div in the shape of a trapezium with clipped overflow

Is there a way to create a trapezium using CSS/Html/Canvas? I have attempted to do so, but my current method is messy and not practical. div { width:0; margin-left:-1000px; height:100px; border-right:1000px solid lightblue; border-top:60px solid tra ...

Adjust the position of elements to prevent overlap

I'm facing a problem with elements overlapping on my website. The navbar is sticky, meaning it slides down the page when scrolling. Additionally, I have a "to-top" button that automatically takes you to the header when clicked. I want the "to-top" but ...

Build interactive web pages using Python scripts

By utilizing the Python scripts provided below, you can generate an HTML file with pre-set global variables (a, b, c, d). However, there might be instances when certain variables are not set globally, resulting in errors like "global 'a' is not d ...

Finding the width or height of an image that is dynamically determined by its proportions using JQuery

Using the "img" tag with only a width value in CSS will automatically calculate the height proportionally. However, finding the height value using developer tools or jQuery's height() method may not work as expected. See the example below: HTML code ...

Filtering URLs using Firefox extension

As the page loads, multiple HTTP requests are made for the document and its dependencies. I am looking to intercept these requests, extract the target URL, and stop the request from being sent if a specific condition is met. Additionally, plugins may als ...

Apply the "ng-class" directive only if there is an <a> element located after the specified element

My issue involves a list of items categorized by labels, with a search filter applied. The problem arises when the labels appear in the search results even though the lists themselves are empty. I need to hide the relevant label if there are no items prese ...

Retrieve the chosen item from the autocomplete feature

Unfortunately, I have encountered an issue with this solution as I was unable to get it to work correctly in my code. The objective is to retrieve the selected item and then call another function to utilize it. The script is as follows: <script type=" ...

Creating an Angular search input and connecting it to an API: A step-by-step guide

As someone who is new to Angular, I have been exploring the functionality of an API that handles server-side filters. After successfully testing it using Postman, I decided to implement a search box to perform the filtering directly from the user interface ...

Occasionally, the resizing of images on the client side and uploading via AJAX can be hit or

Having trouble with code that allows users to upload, resize, and display image files on a WordPress site. The issue is that the code works inconsistently, sometimes failing to upload certain jpeg files or causing the progress bar to fluctuate during the u ...

Organizing Firestore data into structured JSON format

For my database collection, I am utilizing a cloud function to download it as a JSON file. Here is the code snippet: exports.csvJsonReport = functions.https.onRequest((request, response) => { const db = admin.firestore() const userAnswers = db.col ...

Create a horizontal bar graph displaying percentages using React.js

I am attempting to create a horizontal chart using react-chartjs where each data point displays as a percentage. import React, { Component } from "react"; import { HorizontalBar } from "react-chartjs-2"; export default class Button ext ...

In Internet Explorer 9, the cursor unexpectedly moves up above the element

Within my MVC3 application, I have implemented a feature to change the cursor when hovering over an element. Below is the JavaScript code that accomplishes this: $('#print').hover(function () { var cursorUrl = 'url(@Url.Content("~/Cont ...