Challenges with looping in Jquery animations

I've been working on an animation function that I want to run in a loop. So far, I've managed to get it to work for one iteration, but I'm struggling to figure out how to repeat the loop a specific number of times. Below is the code for my animation function. Can someone please help me figure out how to run this any number of times or even make it a never-ending loop?

<script type="text/javascript">
$(document).ready(function(){
    var delay = 150;
    var times = 4;
    var i = 1;

    doMove = function() {
        if( i < times ){
            $('#lip').removeClass('lip'+i);
            $('#lip').addClass('lip'+(i+1));
        } else if ( i == times ) {
            $('#lip').removeClass('lip4');
            $('#lip').addClass('lip1');
        }

        ++i;
        if( i >= times ) {
            clearInterval( interval ) ;
        }
    }

    var interval = setInterval ( "doMove()", delay );


});
</script>

Thank you so much for your help. I'm still new to jquery.

Answer №1

Your script is experiencing a very short delay, making it difficult to see the changes. Consider adjusting the delay time:

var delay = 150;

Change it to:

var delay = 1000; // 1 second

UPDATE

If you want the script to repeat upon completion, here's an example:

Setting repeats = 0 will result in infinite loops, 1 will run once, 2 will run twice, and so on...

var delay = 150;
var times = 4;
var repeats = 3;
var i = 1;
var j = 0;

doMove = function() {
    if( i < times ){
        $('#lip').removeClass('lip'+i);
        $('#lip').addClass('lip'+(i+1));
    } else if ( i == times ) {
        $('#lip').removeClass('lip4');
        $('#lip').addClass('lip1');
    }

    ++i;
    if( i >= times ) {
        if (j < repeats || repeats == 0) {
            i = 1;
        } else {
            clearInterval( interval ) ;
        }
    }
    ++j;
}

var interval = setInterval ( "doMove()", delay );

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

What is the best way to display text from a header box across multiple line boxes in real time on an HTML page?

Looking to enhance an HTML layout with a header box and line comment boxes. <textarea name="order[header_comments]"></textarea> The line comment boxes are structured as follows: <textarea name="line_comments[0][line_comments]"></tex ...

Ajax is making a comeback to the world of text after straying towards

I'm encountering an issue with my code that is supposed to retrieve values from PHP using Jquery AJAX with JSON datatype. However, after the AJAX request is complete, it doesn't display properly in HTML and instead appears as text. Here is what ...

Methods for verifying if a file is included in another file, while disregarding any whitespace adjustments

Let's say I have multiple CSS files (a.css, b.css, ..., e.css) and after concatenating and compressing them, I get a new file called compressed.css. Now, I need to verify if each of the original CSS files is included in the compressed.css file. Are th ...

Is it possible for a Jquery radio button to trigger an infinite loop?

When I click on a radio button, I want to receive an answer from the PHP file. However, when I use the radio button, the alert appears in an endless loop. Why does this happen and how can I make the alert display only once? I tried with just a regular but ...

Refreshing a single HTML element in ASP.NET MVC - the simple way!

Recently, I put together an image gallery using the unite gallery jquery plugin and now I want to change up the images it displays. My plan is to have a button labeled "art" that, when clicked, triggers a function to update the directory path and load ne ...

"Error" - The web service call cannot be processed as the parameter value for 'name' is missing

When using Ajax to call a server-side method, I encountered an error message: {"Message":"Invalid web service call, missing value for parameter: \u0027name\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(O ...

Spacious width that doesn't require a horizontal scrollbar for navigation

My dilemma is with the width of my body and background image. I set the body width to 1920px to match the background-image for the header. However, since I have a narrower wrapper inside the body, the header background's width gets cut off by the wrap ...

achieving initial value to show upon page load

I'm trying to set a default value that is visible when the page loads. My goal is to have the first button always displayed by default in the "display-donation" div whenever someone visits the form. Currently, when the page loads, 10 is highlighted ...

Using JQuery and Ajax, what is the best way to transfer information from a popup form to a PHP page?

I've noticed that my question has been asked a few times already, but I'm still struggling to get my code working. It seems like I'm using the same code as those solutions, so I can't figure out what's going wrong. Currently, I am ...

placing an image within a frame

I am having trouble aligning two divs side by side. I want to create a white background box with the date displayed in the top right corner, and I want the image to be at the same height as the date. Below is the code I have written: #boxx { background-c ...

Having trouble using the `.not` function in jQuery

I'm working on implementing a collapsible menu using jQuery. When any header is clicked, the next sibling (the box) should expand while all other boxes collapse. HTML <div class="finbox" id="finbox1"> <div class="finheader" id="finheade ...

steps to send an array through an ajax request using php

Here is an example of an array: $(document).ready(function(){ var recentchatfriendids = []; // Actual array $.ajax({ url: '/friends/message/getmessagestatus', data: { recentchatfriendids : recentchatfriendids }, succe ...

What are the specifications for the opacity, width, and height of the background image in Bootstrap's

My current project has specific requirements that I need assistance with: I need the background image of the jumbotron to fit the width of the page and have its height scaled proportionally. The opacity of the jumbotron's background image should be ...

Are `<text>` nodes unable to utilize ligature fonts in CSS/SVG?

Check out this JsFiddle demo: http://jsfiddle.net/d0t3yggb/ <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <div class="material-icons">add add add add</div> <svg width="100%" height="100% ...

Revamp the button's visual presentation when it is in an active state

Currently, I'm facing a challenge with altering the visual appearance of a button. Specifically, I want to make it resemble an arrow protruding from it, indicating that it is the active button. The button in question is enclosed within a card componen ...

Modify the appearance of the post-content's color and background when hovering

I am struggling to achieve the goal of changing the background of the search box to transparent and making the search icon white on hover. Any help or suggestions would be greatly appreciated! Below is the code where I want to apply the hover CSS effect: ...

Load data into a table in HTML after a successful jQuery event

I'm facing an issue with populating an HTML table dynamically. Below is the structure of my HTML table: <table class="flexme" id="table" border="1"> <thead> <tr> <th width="100">Usuario</th> <th wid ...

Enhancing menu appearance on an ASP.NET platform

Applying styles to menu items has been a bit tricky for me. When I hover over the container, nothing seems to happen, even though the hover style is applied to the container. The action only takes place when I click on the text of the link. Stylesheet: ...

AJAX retrieving data with a GET request

Looking to customize my page www.domain.com/page.php?x=1&y=2&z=3 Is there a way to assign values to the $_GET variables in an AJAX request using $.get()? $.get('page.php', {x: x, y: y, z: z}, function(){ }) How can I set the $_GET par ...

Incorporating a picture backdrop into a button element in a React Typescript component

I am working on a React project with TypeScript and using a Material UI library. I am trying to set a background image for a button, but when I use src or imageURL, it gives me a TypeScript error. The CSS style also does not show the picture. Here is my ...