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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:13:41+00:00 2026-05-23T21:13:41+00:00

I’m using jQuery and countdown (jQuery plugin). I want the countdown to run for

  • 0

I’m using jQuery and countdown (jQuery plugin). I want the countdown to run for x seconds, where x is an integer from serverTime.php.

Problem – I can’t figure out how to return the responseText in function getTime(), so that it could be used in Timer function.

At first I tried using pure js (without jQuery), and I found similar topics:

  • why-can-i-not-return-responsetext-from-an-ajax-function
  • in-ajax-how-to-retrive-variable-from-inside-of-onreadystatechange-function
  • how-to-return-variable-from-the-function-called-by-onreadystatechange-function
  • and others…

Some ideas/possible solutions I’ve come across:

  • using synchronous call and a global variable for storing response. I
    tried this and don’t consider it an option, because it freezes the
    user screen (and is considered bad practice as well, in most cases)
  • helper function .
  • –

Timer function:

$(function (){
    var time = getTime(); // time is an integer
    $('#defaultCountdown').countdown({until: time, format: 'S', onExpiry: action});
});

getTime() checks serverTime.php and should return the content, which will be an integer.
Alert(html) works, while return html does not work. I know it’s because the call is asynchronous.

function getTime()
{
    $.ajax({
    url: "serverTime.php",
    cache: false,
    async: true,
    success: function(html)
    {
        alert(html);
            helperFunction(html);
        return html;
    }
    });
}

helperFunction. Unfortunately, I don’t know how to pass the result to timer function.

function helperFunction(response)
{
    return response;
}

serverTime.php

echo 30; // 30 seconds, just an example

I’ve tried to explain my problem and show that I’ve put some time into it. If you need additional information, just ask. Maybe I’m just thinking in the wrong direction.

  • 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-23T21:13:41+00:00Added an answer on May 23, 2026 at 9:13 pm

    Your code doesn’t show where the time variable is actually being used.

    Ultimately, you can’t return the AJAX response from the getTime() function because the AJAX is asynchronous, so getTime() will always return before the response is received.

    You’d do better to pass whatever code you want into the getTime() function…

    function getTime( func )
    {
        $.ajax({
            url: "serverTime.php",
            cache: false,
            async: true,
            success: func  // <--set the function received as the callback
        });
    }
    
       // pass a function to getTime, to be used as the callback
    
    function myFunc( html ) { 
       /* do something with html */ 
    }
    getTime( myFunc );
    

    …or call another function from inside the :success callback, passing it the response…

    function getTime( func )
    {
        $.ajax({
            url: "serverTime.php",
            cache: false,
            async: true,
            success: function( html ) {
                someFunction( html ); // <--- call a function, passing the response
            } 
        });
    }
    
    someFunction( html ) {
        /* do something with html */
    }
    

    …or simply hardcode the proper code into the :success callback…

    function getTime( func )
    {
        $.ajax({
            url: "serverTime.php",
            cache: false,
            async: true,
            success: function( html ) {
                /* do something with html */
            } 
        });
    }
    

    EDIT:

    I now see where you want to use time. You could use my second example above. Just be sure that the function you create is in a variable scope that is accessible to your getTime() function.

    Like this:

    $(function (){
        getTime();  // get things started when the DOM is ready
    });
    
    function getTime() {
        $.ajax({
            url: "serverTime.php",
            cache: false,
            async: true,
            success: function(html)
            {
                doCountdown( html );  // call doCountdown, passing it the response
            }
        });
    }
    
       // doCountdown receives the response, and uses it for the .countdown()
    function doCountdown( time ) {
        $('#defaultCountdown').countdown({until: time, format: 'S', onExpiry: action});
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.