www.webdeveloper.com
Results 1 to 9 of 9

Thread: setTimeout parameters lost on 3rd call

  1. #1
    Join Date
    Apr 2010
    Posts
    7

    setTimeout parameters lost on 3rd call

    Hello all,

    I am in the process of creating an ajax like upload control (like the one described here: http://geekswithblogs.net/rashid/arc...le-Upload.aspx ).

    I have ran into an unexpected problem that I can't seem to find a solution to. Javascript is not my favoured tech, and maybe there is something fundamental about this that I don't understand. I would really appreciate if someone could point out what I'm doing wrong:

    I am using setTimeout to basically show a dummy progress bar (by manipulating backcolour of td elements). I start the process like this:

    Code:
    function beginUploadProgress(Cell) {
        _divUploadProgress.style.display = '';
        clearUploadProgress(Cell);
        _uploadProgressTimer = setTimeout(function() { updateUploadProgress(Cell) }, PROGRESS_INTERVAL);
    }
    This initiates properly, and I can see the first 2 td elements being coloured, then I get an error in my updateUploadProgress routine (Cell is undefined):

    Code:
    function updateUploadProgress(Cell) {
        _loopCounter += 1;
    
        if (_loopCounter <= _maxLoop) {
            document.getElementById(Cell + '_tdProgress' + _loopCounter).style.backgroundColor = PROGRESS_COLOUR;
        }
        else {
            clearUploadProgress(Cell);
        }
    
        if (_uploadProgressTimer) {
            clearTimeout(_uploadProgressTimer);
        }
    
        //_uploadProgressTimer = setTimeout(function() { updateUploadProgress(Cell) }, PROGRESS_INTERVAL);
    }
    I'm sure there is nothing happening to the Cell variable, but I have a feeling there is something about the scope of the Cell variable that I don't understand. Anyone have any idea why it might be coming up as 'undefined' for the 3rd call of updateUploadProgress?

    Many thanks for your valuable time.

    Mike

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Insufficient data. We see too many variables defined elsewhere to be able to understand where the error. No, there is nothing with the scope of the Cell variable. (except maybe we don't know what kind of variable is and where was it defined )

    can't you post a link to test page, instead?

  3. #3
    Join Date
    Apr 2010
    Posts
    7
    Apologies.. let me give some more info:

    I have an aspx page which is loaded in an iframe. onload of the iframe I call a function called initFileUpload:

    Code:
    var PROGRESS_INTERVAL = 500;
    var PROGRESS_COLOUR = 'blue';
    var _maxLoop = 10;
    
    function initFileUpload(Cell) {
        _divFrame = document.getElementById(Cell + '_divFrame');
        _divUploadMessage = document.getElementById(Cell + '_divUploadMessage');
        _divUploadProgress = document.getElementById(Cell + '_divUploadProgress');
        _ifrFile = document.getElementById(Cell + '_ifrFile');
    
        var btnUpload = _ifrFile.contentWindow.document.getElementById('UploadButton');
    
        btnUpload.onclick = function(event) {
            var filFile = _ifrFile.contentWindow.document.getElementById('FileToUpload');
    
            //Baisic validation for Photo
            _divUploadMessage.style.display = 'none';
    
            if (filFile.value.length == 0) {
                _divUploadMessage.innerHTML = '<span style=\"color:#ff0000\">Please specify the file.</span>';
                _divUploadMessage.style.display = '';
                filFile.focus();
                return;
            }
    
            beginUploadProgress(Cell);
            _ifrFile.contentWindow.document.getElementById('fileUpload').submit();
            _divFrame.style.display = 'none';
        }
    }
    Cell is a variable (containing a string) which refers to which area of my application where my upload control resides... there can be multiples.

    I've also included my constants.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by HairyMike View Post
    Anyone have any idea why it might be coming up as 'undefined'
    What becomes "undefined"? Are you talking about the variable Cell? Cell is a primitive (if it was defined as a simple string), thus it can not become undefined.

    Who said "undefined"? The browser's error console? Which is the error message? maybe something else is undefined, maybe your 3rd element with that id is undefined, but not the string variable.

    Things remains a little bit unclear for me, i confess...

  5. #5
    Join Date
    Apr 2010
    Posts
    7
    Yes, it has me stumped as well. I am working on a single page ajax app. My code is brought from the server through webservice calls in ASP.NET, like this (not pretty).

    Code:
        Public Shared Function GetUploadControl(ByVal Cell As String, ByVal PathID As Integer) As String
            Dim s As New IO.StringWriter
            s.WriteLine("<div id=""{0}_divFrame"">", Cell)
            s.WriteLine("<iframe id=""{0}_ifrFile"" onload=""initFileUpload('{0}')"" scrolling=""no"" frameborder=""0"" " & _
                        "hidefocus=""true"" style=""text-align:center;vertical-align:middle;border-style:none;" & _
                        "margin:0px;width:100%;hieght:55px"" src=""../system/upload.aspx{1}""></iframe>", Cell, "?path=" & PathID & "&cell=" & Cell)
            s.WriteLine("</div>")
            s.WriteLine("<div id=""{0}_divUploadMessage"" style=""padding-top:4px;display:none""></div>", Cell)
            s.WriteLine("<div id=""{0}_divUploadProgress"" style=""padding-top:4px;display:none;"">", Cell)
            s.WriteLine("<span style=""font-size:smaller"">Uploading file...</span>")
            s.WriteLine("<div>")
            s.WriteLine("<table border=""0"" cellpadding=""0"" cellspacing=""2"" style=""width:100%"">")
            s.WriteLine("<tbody>")
            s.WriteLine("<tr>")
            s.WriteLine("<td id=""{0}_tdProgress1"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress2"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress3"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress4"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress5"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress6"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress7"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress8"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress9"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("<td id=""{0}_tdProgress10"">&nbsp; &nbsp;</td>", Cell)
            s.WriteLine("</tr>")
            s.WriteLine("</tbody>")
            s.WriteLine("</table>")
            s.WriteLine("</div>")
            s.WriteLine("</div>")
            Return s.ToString
        End Function
    I assign the cell when I create the control (it is unique). I am doing my debugging with visual studio 2008.

    The only other code that you haven't seen is the part that handles the file upload. That is working as expected.

    Thanks again..

    I've attached some screenshots of the error:
    Attached Images Attached Images

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    aha... "Object required" means, probably, that element (probably a td element) with that certain id does not exist. Can you test the document in FireFox with the Firebug extension?

    I don't know much about Visual Studio and ASP.NET - only my colleagues work with, as I cover the php/mySQL area... I don't know what to say...
    Last edited by Kor; 04-09-2010 at 10:10 AM.

  7. #7
    Join Date
    Apr 2010
    Posts
    7
    Hi Kor,

    If you look at the screenshot titled 'debug1.jpg' in my previous post, you should be able to see 'undefined' beside the Cell variable. When I break execution of my javascript, visual studio can show me the value of variables that are currently in scope (It is slightly faded in the screenshot, but still visible)..

    I am positive that the element exists. I can put a watch on this:

    Code:
    document.getElementById('TSK_3_0' + '_tdProgress' + _loopCounter)
    and can see that the object definately exists (as you can see in this attached screenshot).

    Thanks for your help.
    Attached Images Attached Images

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Yes, I saw at a second glance. Do you have another Global variable called Cell somewhere?

  9. #9
    Join Date
    Apr 2010
    Posts
    7
    That is what I had initially thought.. but after extensive checking, I can be sure that no other global variables are called 'Cell' (the only globals I've created are the constants for this control).

    I thought maybe Cell might be a system variable, so I tried this:

    Code:
    var myUniqueCellGlobal;
    
    function beginUploadProgress(Cell) {
        _divUploadProgress.style.display = '';
        clearUploadProgress(Cell);
        myUniqueCellGlobal = Cell;
        _uploadProgressTimer = setTimeout(function() { updateUploadProgress(myUniqueCellGlobal) }, PROGRESS_INTERVAL);
    }
    I also changed the name of the params in updateUploadProgress to myCell.

    I've been able to step through this and see that the right value is going into the _uploadProgressTimer's parameter (same as it does when using 'Cell').

    So what is happening is that the function succeeds twice before the variable becomes undefined.. It doesn't seem to matter what I call the variables, the result is always the same.

    Very frustrating...

    But again, thanks for spending the time

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles