开发者

Javascript keeps undefining my vars, it's harshing my buzz. Help?

开发者 https://www.devze.com 2022-12-23 06:32 出处:网络
This is my first experience with javascript, and... Well... Ugh. Here\'s what\'s happening: function step_1(id) {

This is my first experience with javascript, and... Well... Ugh. Here's what's happening:

function step_1(id) {
    //blah blah
    step_2(id);
}
function step_2(id) {
    //blah blah
    step_3(id);
}
function step_3(id) {
    //blah blah
    alert(id);
}

step_1(0); // I can stick any number here, same thing happens...

The alert pops up and says "Undefined". But, if I throw an alert(id); in step_2, then both alerts say "0".

Why/how is id undefined? What am I doing wrong?

I've even tried reassigning id in each function, like:

var nid = id;
step_2(nid);

etc... But that still doesn't work without the alerts.

EDIT: Since my example apparently works fine, perhaps it would help to look at the blah blah that's going on in my code. It works fine, unless I take out the ale开发者_Go百科rt(id); on line 11.


There's a difference between step_2 and step2. And all your other little steps ...


You have a line (line 30) at the end of checkUpload that calls itself without any parameters:

    window.setTimeout('checkUpload();', 333);

It seems like this is what you meant to do:

    window.setTimeout(function() { checkUpload(id); }, 333);

    // which is the equivalent to:
    // window.setTimeout("checkUpload(" + id + ");", 333);


Your script re-calls checkUpload() via a timer without passing along the id parameter it's expecting.

Line 30

window.setTimeout('checkUpload();', 333);

Change to

window.setTimeout( function(){ checkUpload(id); }, 333 );

In the future, as a helpful piece of advice, I encourage you to post your actual problem the first time around, and not a pared down example that you think illustrates the issue. Just saves everyone time and effort ;)


_in the steps may be killing program and your buzz step2 and step_2 are two different things


Mate, I don't know if this answer is relevant as pointed by others, but couple of observations from seeing your code.

if (uploadFrame.contentDocument.readyState == 'complete') {
            if (uploadFrame.contentDocument.getElementById('new_image_id')) {
                    var new_id = uploadFrame.contentDocument.getElementById('new_image_id').innerHTML;

Really! you should know that '==' is not the correct operator but '===' is. A common mistake made by js learners/users. Also you may want to recheck the id, is it 'new_image_id' or 'new_image_'+id.

0

精彩评论

暂无评论...
验证码 换一张
取 消