Create an animation that loops continuously

I've been attempting to create a repeating animation with jQuery by using a while loop where the condition is set to true. However, it seems that this approach is not appreciated by the browser.

Is there a proper way to achieve this type of animation?

Check out my code snippet on jsFiddle here!

$('.test').click(function () {
    while (true) {
        $(this).hide(1000);
        $(this).show(1000);
    }
});

Answer №1

Here is a simple solution:

$('.test').click(handleClick);

function handleClick(){
    $(this).hide(1000).show(1000, handleClick);
}

Check out this code on JSFiddle

This jQuery code utilizes queue animation.

Answer №2

If you want to create a loop in a function, you will need to have the function call itself as a callback.

http://jsfiddle.net/8cV2R/8

$('.test').click(fadeIt);

function fadeIt() {
    $('.test').hide(1000, function() {
        $('.test').show(1000, fadeIt);
    });
}

Answer №3

function hideElement() { $(this).hide(1000, showElement); };
function showElement() { $(this).show(1000, hideElement); };
$('.test').click(hideElement);

Check out the jsFiddle example here.

If you prefer a blink effect without the width/height animation that hide/show creates when specifying a duration, you can try this alternative: View the other jsFiddle example here.

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

Styling and structuring your website with CSS and HTML

I am coding an HTML page where I've incorporated CSS for the styling. However, I'm facing an issue with the drop-down menu. Whenever I try to click on a drop-down option, it disappears. How can I fix this? I want the drop-down options to be clic ...

Python Django: Implementing Proper Spacing in HTML Email Titles

We are currently working on integrating the email feature using Python Django, but we are encountering an issue with extra spaces appearing in the header of the emails as shown in these screenshots: https://i.sstatic.net/EpCca.png https://i.sstatic.net/q6 ...

Using JQuery to implement draggable text on an image

Looking for a solution that allows users to easily add a watermark text anywhere on an image. I'm envisioning a movable text box functionality where they can freely position the text and change its content. Any suggestions for a JQuery plugin that can ...

Using an AJAX post request will result in receiving an HTML element, especially when an attachment is included with Paperclip

I've created an innovative application where users can share their stories through ajax submissions and engage with other readers by commenting on the stories using ajax as well. The technology stack I'm working with includes Rails 3.0.3, ruby 1. ...

Liquid code in Shopify: Eliminate every product that customers have added to the Cart Page when they click the "Submit" button

I have a unique Submit Button design, and I am looking to clear all products that have been added to the Cart Page when this button is clicked: <button class="button_get order_button btn btn-pink js_submit button__text orderButton" id="send" type="subm ...

Instead of overlapping the content, my overlay shifts the entire page downwards

I am looking to create a dynamic user experience where clicking on an image triggers a <div> overlay that darkens the background. However, instead of simply appearing over the current screen, I want the overlay to push everything else beneath it. Be ...

My PHP errors and success messages are not being displayed properly after an AJAX success

After making an AJAX call to submit a form, I would like to display either the PHP success message or error message upon completion. This is my current AJAX success function: success: function (data) { resultSuccess = $(data).find("#success") ...

Using the Ajax method from a separate class in TypeScript: A step-by-step guide

Recently, I started learning about typescript and ajax. One of the challenges I encountered was while creating a method in typescript for making ajax calls that can be used across classes: myFunc(value: string): JQueryPromise<any> { var dfd = $. ...

Struggling to make the bootstrap navigation bar function properly

I've been experimenting with replicating the basic framework to learn more about responsive web design using bootstrap. However, I've only been able to get certain parts to function correctly. For instance, I can use the "btn" class without any i ...

Ryan Fait's Code for Creating a Responsive Sticky Footer Height

There are quite a few queries regarding Ryan Fait's sticky footer, but I urge you not to dismiss this one immediately! My goal is to have a footer with a dynamically sized height that sticks to the bottom of the page. Ryan Fait suggests wrapping all ...

Tips for effectively using the Susy +pad mixin

How can I use the +pad method to add padding on a p element without extending it past a certain point? I've tried using +pad, but the p always goes beyond the desired stopping place. Take a look at this screenshot for reference: The issue arises when ...

What is the best method for modifying an XML document without altering its associated XSL stylesheet?

I am working with XML data retrieved from a URL: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="/style.xsl"?> ....etc... To display the data in HTML format, I added the second line referencing the style.xsl file ...

Error message: ASP.Net Update Panel unable to access global variables in code-behind

I have been working on updating a bootstrap progress bar's percent on my website. While I understand that ajax is the recommended method for this, I attempted to achieve this without using ajax and was able to make some progress. Below is the code sn ...

"Upon refreshing the page, the onclick event ceases to

What I'm hoping to achieve: I have a div that is used to display user statistics. When a user is clicked on, the details of that user are loaded into the div using an ajax request. Every 10 seconds, the div refreshes and displays the stats of all use ...

Apply CSS code to create a fading effect for a container

Below is the code for my accordion list, created using only CSS and HTML. Whenever a heading is clicked, the text slides in with a different background color. How can I make it so that the container with the text and different background color fades in and ...

Discount levels determined by the selection of product radio buttons within WooCommerce

I made a modification to my theme's functions.php file by adding the following code snippet. This code snippet is designed to provide a small discount based on quantity for a specific product with the name containing "Hydrogel". However, I am encount ...

Developing dynamic tabs with AJAX updates on the frontend in Magento

I'm looking to create ajax updated tabs for the user profile tab on the frontend of Magento, similar to the categories tab in product management. Here is the code I have used: <ul id="page_tabs" class="tabs"> <li> <a id="p ...

My navbar list elements are not functioning properly due to an issue with JS. Does anyone have a solution to resolve this issue

As I work on creating a website using a free template, I encountered an issue with JS that I am not very familiar with. After conducting some research, it seems like the problem lies within the JS code. Below are the relevant snippets of HTML and JS code: ...

After creating a new canvas, the HTML5 canvas does not appear on the screen

Greetings! I am a newcomer to this forum, but I have utilized it for answers in the past. Currently, I am working on a school project that involves using HTML5, CSS, and JS to create teaching lessons. My approach includes utilizing canvas to develop intera ...

Mobile display trapping Bootstrap navbar

Can anyone help me figure out why my navbar is stuck in its mobile display? Here is the link to the code: http://jsfiddle.net/enL35t1q/ Navbar <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul cla ...