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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:00:13+00:00 2026-05-19T13:00:13+00:00

I have two functions, one that makes an Ajax request when the user loads

  • 0

I have two functions, one that makes an Ajax request when the user loads the page, and one that will run every 5 or so seconds to update something. Using the first function, I can output a variable that I need to use in the second function.

function insert_last_ten() {
    $.ajax({
       url: 'freeshout/chatlog.php',
       success: function(data) {
         $("#inner-wrap").html(data);
         var first_child = $("#inner-wrap :first-child").html();
         var value = first_child.match(/(value)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/);
         var realtime = value[2];
       }
     });
    }

Basically, I need to use realtime to do something else in another function. For the sake of simplicity, let’s pretend this is the second function:

function update() {
    alert(realtime);
}

How could I go about making that work?

  • 1 1 Answer
  • 3 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-19T13:00:14+00:00Added an answer on May 19, 2026 at 1:00 pm

    In the success callback, cancel the timeout and start a new one using the updated value. You can pass the timeout identifier to insert_last_ten via argument and the success callback will pick it up via closure:

    function createUpdateTimer(value, interval) {
        return setTimout(
          function () {
            alert(value); // The created function knows what value is due to closure
          }, interval);
    }
    
    function insert_last_ten(timer) {
        $.ajax({
            url: 'freeshout/chatlog.php',
            success: function(data) {
                $("#inner-wrap").html(data);
                var first_child = $("#inner-wrap :first-child").html();
                var value = first_child.match(/(value)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/);
                var realtime = value[2];
                cancelTimer(timer); // This callbac knows what timer is due to closure
                timer = createUpdateTimer(realtime, 500);
            }
        });
    }
    
    // Start the timer:
    var timer = createUpdateTimer('initial value', 500);
    
    // Make ajax request:
    insert_last_ten(timer);
    

    Note that I am only beginning to familiarize myself with the good parts of JavaScript. This code is untested.

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

Sidebar

Related Questions

I have two PHP functions - one that builds a html form and another
I have two functions whose underlying logic is the same but in one case
i have two data classes which hold only data members(no functions). One is CallTask
I have two functions that have different enough logic but pretty much the same
I have two functions that are supposed to encrypt and decrypt a string but
I have a page that has a 4x4 grid and two squares change color
I have a bit of javascript that I have used successfully on one page,
I have a bit of javascript that I have used successfully on one page,
i have two php files where one is executed through url and that file
To have an indicator while doing an ajax request, I got information saying that

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.