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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:43:16+00:00 2026-06-02T01:43:16+00:00

So I found this good example of long-polling, but I couldn’t figure out how

  • 0

So I found this good example of long-polling, but I couldn’t figure out how to transfer variables through it. This is what I came up with, but it doesn’t work. I’m pretty confident the issue is with the dataString and type, because that’s the part of the code I modified.

$(".post").each(function() {
    poll("somestuff");
});


(function poll(pid) {
    var dataString = 'pid=' + pid;
    $.ajax({
        type: 'GET',
        url: 'http://localhost:8888/site/execs/something.php',
        data: dataString,
        success: function(data) {

            alert('stuff');


        },
        dataType: "json",
        complete: poll,
        timeout: 30000
    });
})();​

Can someone tell me what it is I’m doing wrong here? Any help is appreciated, thank you.

  • 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-02T01:43:19+00:00Added an answer on June 2, 2026 at 1:43 am

    Regarding the scope of poll, hegemon’s answer is mostly correct – the function is not global. However, there’s more to it.

    The way you’ve written poll makes it what’s known as a named function expression.

    Remember that there are two ways of writing functions in JavaScript. The traditional function declaration:

    function foo() {
        ...
    }
    

    Declarations must be named, and are hoisted to the top (basically, parsed before any instructions are executed). Alternatively, function expressions:

    var foo = function() {
        ...
    }
    

    Or

    $.ajax('/', function() {
        // this is what's known as an anonymous callback
    });
    

    Or

    (function() {
        // this is called a self-executing function...
    })(); // <-- because we call it immediately
    

    Expressions are executed like any other code; they are not hoisted.

    And now the fun part: function expressions may be given an optional name, but that name is not accessible outside the scope of the function itself. In other words,

    (function foo() {
        // `foo` is this function
    });
    
    // `foo` will be undefined here
    

    Would be much like writing this:

    (function () {
        var foo = arguments.callee; // never do this
        // `foo` is this function
    });
    
    // `foo` will be undefined here
    

    Because of the fact that a named function expression can only call itself (or be called by a function declared inside its scope), plus a whole host of browser bugs, named function expressions are virtually useless outside of adding some context in a debugger or profiler.


    So now let’s walk through your code.

    First, you walk through each element that has a post class. jQuery immediately invokes your anonymous callback for each matching element. You try to call poll, but it:

    1. Doesn’t exist yet because function expressions are not hoisted; the poll code has not run yet.
    2. Even if it had already run (say, if you moved the $.each call to the bottom), post would still be undefined because poll is a named function expression, and we just learned that those identifiers are only available inside of the function itself.

    Next, you have a self-executing function. (The () on the very last line.) In other words, poll is invoked immediately with no arguments. You’re probably seeing a single long-poll sending pid=undefined.

    Bonus: The complete callback (to begin a new long-poll after one times out or gets data) does work as expected, since poll is properly in scope at that point.

    Fixing all this nonsense is as easy as removing three characters. Hopefully, by this point you’re able to figure out which three characters those are. (Hint: make your expression a declaration.)

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

Sidebar

Related Questions

So I can't seem to find a good example of this anywhere. I found
I found some good answers for this question, but I can't really get them
i know every web-developer hates this topic, but anyway... i found no good solution
This problem pops around the network for years, I've found no good solution yet.
I don't know how to do this, and I've found no good resources online
found this little code snippet that seems to do what i want, but im
Found this Multimap containing pairs? , but it is not much help How would
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
I found a really good example about boolean translator, * Boolean expression (grammar) parser
I have a very simple long polling ajax call like this: (function poll(){ $.ajax({

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.