admin 管理员组文章数量: 1086019
I've included jquery in my script and am now trying to make a jquery version of this upload progress bar script: /
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?
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./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?
- there is the error of setTimeout function syntax so correct that syntax it will work perfectly. – Dhaval Bharadva Commented Jul 24, 2013 at 10:00
5 Answers
Reset to default 3Haven'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;
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);
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
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. So to use it, just move it to global.
本文标签: Javascript function not defined within documentready jquery codeStack Overflow
版权声明:本文标题:Javascript function not defined within document.ready jquery code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744017183a2519133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论