Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:50:01+00:00 2026-06-17T08:50:01+00:00

I have a function that runs a loop using setInterval. The function empMove decreases

  • 0

I have a function that runs a loop using setInterval. The function empMove decreases the margin-top by -182px each time. I have another if statement within this to check when it needs to end. In here I use clearInterval to stop the loop, and I set the margin-top back to zero.

The problem is that it stops but does not restart again.

Sorry I didn’t paste the entire code but that would be huge and distract from the focus of the issue.

// if statements to move carousel up
$carouselNum = $('.carousella').length;

$loopNum = $($carouselNum * -182);

if($carouselNum  > 1){

    // function empMove, the '-=' allows the -margin-top to run every time. Without this it will set the margin-top to the same value every loop
    var myLoop = setInterval(empMove, 2500);
    function empMove() {
        $('.emp-wrap').css('margin-top', '-=182');
        console.log('loop start');
        var marginTop = $('.emp-wrap').css('margin-top');
        console.log(marginTop);
        if(marginTop = $loopNum){
            // do something
            clearInterval(myLoop);
            $('.emp-wrap').delay(2500).css('margin-top', '0');
            console.log('loop stops');
            };
    };    

}
else{
    // do something
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:50:01+00:00Added an answer on June 17, 2026 at 8:50 am

    You need to call setInterval again.

    myLoop = setInterval(empMove, 2500);
    

    Since, clearInterval will destroy it.

    Technically, you could do it after setting the margin-top back to 0.

    clearInterval(myLoop);
    $('.emp-wrap').delay(2500).queue(function(next){
      $('.emp-wrap').css('margin-top', '0');
      myLoop = setInterval(empMove, 2500);
      next();
    });
    

    But, it will be cleaner if you modify empMove to either decrease the margin or reset it to 0 depending of the if statement.

    var $empwrap = $('.emp-wrap');
    var myLoop = setInterval(function() {
      var marginTop = $empwrap.css('margin-top');
      if (marginTop >= loopNum) {
        $empwrap.css('margin-top', '0');
      } else {
        $empwrap.css('margin-top', '-=182');
      }
    }, 2500);
    

    Disclaimer: untested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using VB 2010 Express. I have a loop that calls a function named
I have the following situation: I have a certain function that runs a loop
Inside my Controller i have function that runs after user clicks on item, which
I have a setup function that runs onload to add some behaviours to elements.
I have a javascript function that runs every 3 seconds, is there any way
I have a function that takes an input string and then runs the string
I have this function that takes the input of a, runs a calculation and
In Winforms you just have the Main function and that's what runs first, but
I have a function that I am calling that runs all the way up
I have a function that I want to loop. I found the easiest way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.