简体   繁体   English

未在document.ready jQuery代码中定义的Javascript函数

[英]Javascript function not defined within document.ready jquery code

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/ 我已在脚本中包含jquery,现在正尝试制作此上传进度条脚本的jquery版本: 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. 我试图将功能放到document.ready() ,但没有帮助。 I even went and defined getProgress at the beginning of the inside of startProgress but it doesn't seem to recognize the function. 我什至在startProgress内部的开头定义了getProgress ,但似乎无法识别该功能。 What am I doing wrong? 我究竟做错了什么?

getProgress() is defined within the scope of the callback to document.ready() . getProgress()document.ready()的回调范围内定义。 If you pass a string argument to setTimeout() this is evaluated in the global scope. 如果将字符串参数传递给setTimeout()则会在全局范围内对其进行评估。 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);

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;

if you use the setTimeout function like 如果您使用setTimeout函数,例如
setTimeout('getProgress("' + uid + '")',500) , setTimeout('getProgress("' + uid + '")',500)

you must put the function getProgress in global scope , if you use setTimeout function like setTimeout( getProgress(uid),500) , 如果使用setTimeout函数(如setTimeout( getProgress(uid),500) ,则必须将函数getProgress放在全局范围内,

you can define the function getProgress inside jQuery ready function 您可以在jQuery ready函数中定义函数getProgress

Please use: 请用:

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

That should work fine. 那应该工作正常。

function getProgress(uid) is defined inside $(document).ready() so that it is in the private scope not in global scope. function getProgress(uid)是在$(document).ready()内部定义的,因此它位于私有范围内,而不位于全局范围内。 So to use it, just move it to global. 因此,要使用它,只需将其移至全局即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM