JavaScript Javascript function not defined within document.ready jquery code javascript function defined twice,javascript function defined after call,javascript

I've included jquery in my script and am now trying to make a jquery version of this upload progress bar script: http://www.ultramegatech.com/2008/12/creating-upload-progress-bar-php/

Here's my attempt:

$(document).ready(function () {

    function startProgress(uid) {
        console.log("starting progress");
        setTimeout('getProgress("' + uid + '")', 500);
        //some more stuff
    }

    function getProgress(uid) {
        console.log("getting progress");
        $.ajax({
            type: "GET",
            url: 'upload_getprogress.php?uid=' + uid,
            success: function (msg) {
                progress = msg;
                setTimeout('getProgress("' + uid + '")', 100);
                // do some more stuff
            }

        });
    }

    $('#upload').submit(function () {
        startProgress('<?php echo $uid; ?>');
    });
});

But I'm getting this error:

Uncaught ReferenceError: getProgress is not defined

How is that?

I tried to put the functions outside of document.ready(), but it didn't help. I even went and defined getProgress at the beginning of the inside of startProgress but it doesn't seem to recognize the function. What am I doing wrong?

Answer:1

Haven't been able to double-check, but I'm guessing it's because of the scope of the submit callback. Try something along these lines;

$(document).ready(function(){   
    $('#upload').submit(function(){ window.startProgress('<?php echo $uid; ?>'); });
});

var startProgress = function(uid) {
       console.log("starting progress");
       setTimeout('getProgress("' + uid + '")', 500);
       //some more stuff
};

var getProgress = function(uid) {
    console.log("getting progress");
    $.ajax({  type: "GET", 
        url: 'upload_getprogress.php?uid=' + uid, 
        success: function(msg) {   
            progress = msg;
            setTimeout('getProgress("' + uid + '")', 100);
                    // do some more stuff
        }

    });
};

window.startProgress = startProgress;
window.getProgress = getProgress;
Answer:2

getProgress() is defined within the scope of the callback to document.ready(). If you pass a string argument to setTimeout() this is evaluated in the global scope. So you method is not visible from there.

You could change your code, to use an anonymous function like this:

 setTimeout( function() {
   getProgress( uid); 
 }
 , 100);
Answer:3

if you use the setTimeout function like
setTimeout('getProgress("' + uid + '")',500),

you must put the function getProgress in global scope , if you use setTimeout function like setTimeout( getProgress(uid),500),

you can define the function getProgress inside jQuery ready function

Answer:4

Please use:

setTimeout(function() { getProgress( uid ); }, 500 )

That should work fine.

Answer:5

I have some strings like this: var st1 = 'AU$99.95'; // Should return 99.95 var st2 = 'AU.99.95'; // Should return 99.95 var st1 = 'Blahblah $99,000'; // Should return 99,000 My current regex gets ...

I have some strings like this: var st1 = 'AU$99.95'; // Should return 99.95 var st2 = 'AU.99.95'; // Should return 99.95 var st1 = 'Blahblah $99,000'; // Should return 99,000 My current regex gets ...

  1. javascript regex remove special characters
  2. javascript regex remove non numeric
  3. javascript regex remove spaces
  4. javascript regex remove html tags
  5. javascript regex remove string
  6. javascript regex remove punctuation
  7. javascript regex remove parentheses
  8. javascript regex remove dashes
  9. javascript regex remove all spaces
  10. javascript regex remove newline
  11. javascript regex remove numbers
  12. javascript regex remove leading zeros
  13. javascript regex remove special characters and spaces
  14. javascript regex remove double quotes
  15. javascript regex remove characters from string
  16. javascript regex remove comma
  17. javascript regex remove multiple characters
  18. javascript regex remove substring
  19. javascript regex remove empty lines
  20. javascript regex remove double spaces

I currently have set data-theme a for my navbar, but I want to be able to set it to a different colour. how do I do this? the navbar is inside the header. How do I override jquery mobile's css? <...

I currently have set data-theme a for my navbar, but I want to be able to set it to a different colour. how do I do this? the navbar is inside the header. How do I override jquery mobile's css? <...

I want to update my table with javascript every couple seconds. So far I made an ajax post request to my update.php and trigger if is set. Then do a mysql query and put the resultset in a json ...

I want to update my table with javascript every couple seconds. So far I made an ajax post request to my update.php and trigger if is set. Then do a mysql query and put the resultset in a json ...

  1. ajax post variable to php
  2. ajax post variable
  3. ajax post variable not working
  4. ajax post variable empty
  5. ajax post variables jquery
  6. ajax post parameters
  7. ajax post params
  8. ajax post parameters jquery
  9. ajax post parameters example
  10. ajax post parameters javascript
  11. ajax post parameters to controller
  12. ajax post parameters in url
  13. ajax post parameters null
  14. ajax post parameters array
  15. ajax post parameters c#
  16. ajax post parameters json
  17. ajax post parameters asp.net
  18. ajax post parameters mvc
  19. ajax post data variable
  20. jquery ajax post variable to php

let's imagine I've declared some styles (either directly on my html document or by external css file(s)): <style> .Red { background-color: red; } .Green { background-color: green; } .Blue { ...

let's imagine I've declared some styles (either directly on my html document or by external css file(s)): <style> .Red { background-color: red; } .Green { background-color: green; } .Blue { ...