日期:2014-05-16  浏览次数:20572 次

求助:setTimeout无效
JScript code
<script type="text/javascript">
        var fileName = null;
        var oldFileName = null;
        setFileOnChange();
        
        function upload() {
            var oldAction = $('form_1').action;
            $('form_1').action = "fileupload/fileUploadServlet.do";
            $('form_1').method = "post";
            $('form_1').enctype = "multipart/form-data";
            $('form_1').target = "iframe_hidden";
            oldFileName = getFileName();
            $('form_1').submit();
            addImg();
            $('form_1').action = oldAction;
            $('form_1').enctype = '';
            $('form_1').target = 'self';
        }

        function getFileName(){
            return fileName;
        }
        
        function setFileOnChange(){
            $('file_img').onchange = function(){
                fileName = this.value;
            }
        }
        
        function isNewFile(){
            if(oldFileName!=null && oldFileName != fileName){
                return true;
            }else{
                return false;
            }
        }
        
        function addImg(){
            if(alreadyExist()&&isNewFile()){
                changeImg();
            }else{
                append();
            }
        }
        
        function changeImg(){
            var node = $('img_1');
            var oldSrc = node.src;
            var newSrc = getImgHref();
            if(oldSrc != newSrc){
                node.src = newSrc;
            }else{
                setTimeout('changeImg()',1000);
            }
        }
        
        function append() {
            var _src = getImgHref();
            if(_src!=null){
                var node = document.createElement("img");
                node.id='img_1';
                node.src = _src;
                node.width = "85";
                node.height = "100";
                $('td_img').appendChild(node);
            }else{
                setTimeout('append()',1000);
            }
        }
        
        function alreadyExist(){
            var node = $('img_1');
            if(node != null){
                return true;
            }
            return false;
        }

        function $(nodeId) {
            return document.getElementById(nodeId);
        }

        function getIframe() {
            var doc = null;
            if (document.all) {//IE
                doc = document.frames["iframe_hidden"].document;
            } else {//Firefox    
                doc = document.getElementById("iframe_hidden").contentDocument;
            }
            return doc;
        }

        function getImgHref() {
            var node = getIframe().getElementById('hidden_imgHref');
            if(node!=null){
                var v = node.value;
                return v;
            }
            return null;
        }
    </script>

主要是用来添加img节点的,应该递归的函数现在就执行了一次.不知道这setTimeout哪出了问题.跪求高手解答.

------解决方案--------------------
看你判断条件。。你不是写在else 里面了嘛。。