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

  • Home
  • SEARCH
  • 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 959203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:59:14+00:00 2026-05-16T00:59:14+00:00

Can jQuery be used as a sleep() or wait() function? Suspending the execution of

  • 0

Can jQuery be used as a sleep() or wait() function? Suspending the execution of the statements after the wait. I tried $().delay(5000) but there was no 5 second wait. Is delay() only used in effects?

I am not looking for solutions which involve setTimeout delayed execution of another function or a CPU hogging solution. I want a sleep() function which can be reused in different scripts.

Addition:

I didn’t mean to suggest a solution which doesn’t use setTimeout at all. I have seen solutions which required to move all code after where the delay is needed into its own function so that setTimeout can call it. I don’t want that. Either a self contained wrapper function for using setTimeout or use jQuery delay() in a dummy non visual effect just for the purpose of simulating a sleep function.

  • 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-05-16T00:59:15+00:00Added an answer on May 16, 2026 at 12:59 am

    Javascript (and by extension, jQuery) doesn’t have a real “sleep” function. You can more or less abuse setTimeout & setInterval (and their derivatives, like delay) to simulate sleep, but you’re usually better off just adapting to the setInterval/Timeout way of doing things… figure out what you want done, seal it off in a function, pass it as an argument to one of those two.

    Edit:

    Okay, I see your edit. You really want a conventional sleep function. What follows is absolutely an abuse and not at all what I’d recommend. But if I had to build one to save my life, here’s how I’d do it: test how long a high number of loop cycles takes, do a statistically significant number of these tests, average the results, and use that information to define a function that roughly translates a given number of milliseconds into a number of loop cycles, which it then runs through.

    In JavaScript:

    function timeloop(cycles) { 
        var start = new Date().getTime(); 
        for(var j=0; j<cycles; j++); 
        var end = new Date().getTime(); 
        return end-start; 
    }
    
    
    var sleep = (function () {
        var ms_tally = 0, i, s = 100, L = 10000;
    
        for(i=0; i<s; i++) 
            ms_tally += timeloop(L);
        var avg = ms_tally / i;
        var ms_per_cycle = avg / L;
        var cycles_per_ms = 1 / ms_per_cycle;
    
        return eval('function (ms) { for(var k=0, z=ms*'+cycles_per_ms+'; k<z; k++); }');
    })()
    

    I wouldn’t trust my life, health, or even $20 to the accuracy of the timing, though. If you need any degree of precision finer than half a second, the right way to do this in JavaScript is to trust the facilities built into whatever environment you’re using… and in the browser, that’s setInterval and setTimeout.

    Edit #2:

    I see the recommendation for Ben Alman’s doTimeout plugin in one of the comments. After looking it over, it’s worth noting that it is a better idea than the abomination I have produced above in just about every way. It isn’t quite the comfortable sleep-y syntax we’re often used to, and you still have to think about your code in functions in some marginal way, but at least you can still keep the sequential feel. If you can’t bring yourself to use setTimeout/Interval directly, use doTimeout or something like it.

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

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 485k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Turns out its just obj.location = new Doctrine_Expression("Point(10, 10)"); but… May 16, 2026 at 7:41 am
  • Editorial Team
    Editorial Team added an answer Here's a cite from Tomcat 7's Jasper documentation: keepgenerated -… May 16, 2026 at 7:41 am
  • Editorial Team
    Editorial Team added an answer The Visual Studio 2010 database project properties "Deploy" tab contains… May 16, 2026 at 7:41 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.