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 7961697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:07:14+00:00 2026-06-04T05:07:14+00:00

setTimeout(function() { console.log(1); } console.log(2); Basically, what I want to output 1 before 2.

  • 0
setTimeout(function() {
    console.log("1");
}
console.log("2");

Basically, what I want to output “1” before “2”. How do I synchronize the callback function with the current “caller” thread?

  • 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-04T05:07:16+00:00Added an answer on June 4, 2026 at 5:07 am

    You can’t hold up the thread in JavaScript (other than the alert and confirm built-ins), so the only ways to make your console.log("2") happen after your console.log("1") are:

    1. Put it in the timeout function:

      setTimeout(function() {
          console.log("1");
          console.log("2");
      }, delay);
      

      …or in another function that that timeout function calls, although as you already have a function for the setTimeout, it’s unclear (other than code organization) why you’d need a separate one.

    2. Put it in a separate function you pass to setTimeout with a longer delay:

      setTimeout(function() {
          console.log("1");
      }, delay);
      setTimeout(function() {
          console.log("2");
      }, longerDelay);
      

      …being careful that longerDelay really is sufficiently longer than delay that you don’t end up with a bit of chaos around scheduling.

    Note I said “the thread” above. Unless you’re using web workers, which have a specific syntax, JavaScript on browsers is single-threaded. Two JavaScript functions cannot run simultaneously, and aside from edge case browser bugs around alert and ajax completions and the like (at least some versions of Firefox run your ajax completion callback while you have a function waiting on alert; strange but true, and nothing you can rely on cross-browser or even cross-version), you can’t pause one JavaScript function in its tracks while another JavaScript function runs.

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

Sidebar

Related Questions

When I call set timeout it returns a number, for example > setTimeout(function(){ console.log('done');
Running the following code: for (var i=0; i<3; i++) { setTimeout( function() { console.log(i);
with the follow code: $('someelement').hover( function() { console.log('mouseenter begin'); setTimeout(function() { console.log('mouseenter ends'); },
var async = require('async'); function f1() { setTimeout(function(){ console.log('111');},300); } function f2() { setTimeout(function(){
I want to trigger the setTimeout callback function, but it seems not work. What's
This question is more on javascript principle. function done(){ console.log('done defined with `function done(){
Check out this code : <a href=# id=link>Link</a> <span>Moving</span> $('#link').click(function () { console.log(Enter); $('#link').animate({
Given this var: somevar = {dothis: function(){console('yay')}}; If I want to hijack it, I
The recursive setTimeout function getRandomProducts is called onload in the html body tag, and
i need to work with setTimeout function but that function does not work. First

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.