2

I am trying to call showUpload(); from within two setTimeouts. Neither works. It seems to be out of scope and I'm not sure why. I tried this.showUpload() which didn't work either.

 $(document).ready(function(){
            var progress_key = $('#progress_key').val();

            // this sets up the progress bar
            $('#uploadform').submit(function() {
               setTimeout("showUpload()",1500);
               $("#progressbar").progressbar({ value:0}).fadeIn();
            });


            // uses ajax to poll the uploadprogress.php page with the id
            // deserializes the json string, and computes the percentage (integer)
            // update the jQuery progress bar
            // sets a timer for the next poll in 750ms
            function showUpload() {
                $.get("/myid/videos/uploadprogress/" + progress_key, function(data) {
                    if (!data)
                        return;

                    var response;
                    eval ("response = " + data);

                    if (!response)
                        return;

                    var percentage = Math.floor(100 * parseInt(response['bytes_uploaded']) / parseInt(response['bytes_total']));
                    $("#progressbar").progressbar({ value:percentage})

                });
                setTimeout("showUpload()", 750);
            }
        });

Thank you for your time.

2 Answers 2

2

As @Daniel said, this should work:

setTimeout(showUpload, 750);

Please note that the quotes should be removed (this is why it isn't being executed until the timeout runs out). Right now, you are passing a string, which is evaled when the timeout runs out. This eval will happen in a different scope, which is why you are seeing the problem you are seeing.

Instead, passing a reference to the showUpload function to setTimeout will allow your function to be executed later. Keep in mind that when it runs, it will be in a different scope, so you may have other scope issues, like with progress_key. You will need to create a closure around showUpload to capture that parameter.

Sign up to request clarification or add additional context in comments.

Comments

2

It looks like you need to remove the parenthesis from showUpload in both your setTimeout calls. Otherwise you will be invoking the showUpload method instead of passing it as a parameter:

setTimeout(showUpload, 750);

2 Comments

The intent is right, though the description is a bit off, you're calling the method with the parenthesis, but you're trying to invoke a method in the global space when it's a string...where it doesn't exist, the direct reference like you have is the correct approach.
@Nick: Thanks for the note... I was just looking for some references on how setTimeout() evals code when passed as a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.