zoukankan      html  css  js  c++  java
  • 关于alert后,才能继续执行后续代码问题

    如果在正常情况下,代码要在alert之后才执行,解决办法:将要执行的代码用setTimeout延迟执行即可(原因:页面未加载完毕)

    首先,先说明问题情况:

     如下JS代码,不能正常执行,只有在最前面加上alert("")后才能执行,

     其实是DOM渲染时间太长,alert起到了延时作用,所以alert后才能看到代码执行效果

    原始代码:

    <script type="text/javascript">
    var
    ctx='${ctx}'+'/static/js/'; var files = []; var errors = []; var type = 'file'; var max_file_size = '10mb'; var filters = {title : "文档", extensions : "xml"}; $("#uploader").pluploadQueue($.extend({ runtimes : 'flash,html4', url : 'localUpLoadProgram', max_file_size : max_file_size, file_data_name:'file', unique_names:true, filters : [filters], flash_swf_url : ctx+'plupload/plupload.flash.swf', init:{ FileUploaded:function(uploader,file,response){ if(response.response){ var rs = $.parseJSON(response.response); if(rs.status){ files.push(file.name); }else{ errors.push(file.name); } } }, UploadComplete:function(uploader,fs){ //var e= errors.length ? ",失败"+errors.length+"个("+errors.join("、")+")。" : "。"; // alert("上传完成!共"+fs.length+"个。成功"+files.length+e); } } },(true ? {chunk_size:'1mb'} : {})));
    </script>

    解决后代码:

    延时执行该函数,就ok了,就达到了alert延时的效果了

    <script type="text/javascript">
        setTimeout(loaderLocal,1);
        function loaderLocal(){
            var ctx='${ctx}'+'/static/js/';
            var files = [];
            var errors = [];
            var type = 'file';
            var max_file_size = '10mb';
            var filters = {title : "文档", extensions : "xml"};
            $("#uploader").pluploadQueue($.extend({
                runtimes : 'flash,html4',
                url : 'localUpLoadProgram',
                max_file_size : max_file_size,
                file_data_name:'file',
                unique_names:true,
                filters : [filters],
                flash_swf_url : ctx+'plupload/plupload.flash.swf',
                init:{
                    FileUploaded:function(uploader,file,response){
                        if(response.response){
                            var rs = $.parseJSON(response.response);
                            if(rs.status){
                                files.push(file.name);
                            }else{
                                errors.push(file.name);
                            }
                        }
                    },
                    UploadComplete:function(uploader,fs){
                        //var e= errors.length ? ",失败"+errors.length+"个("+errors.join("、")+")。" : "。";
                       // alert("上传完成!共"+fs.length+"个。成功"+files.length+e);
                    }
                }
            },(true ? {chunk_size:'1mb'} : {})));
        }
    </script>
  • 相关阅读:
    day5-Python学习笔记(九)json数据类型
    day5-Python学习笔记(八)内置函数
    day4-Python学习笔记(七)函数与模块
    day4-Python学习笔记(六)监控日志,集合数据类型
    day4-Python学习笔记(五)文件读写,文件内容修改
    day3-python学习笔记(四)字符串方法
    day3-python学习笔记(三)字典、元组
    day3-python学习笔记(二)list(数组)
    变量
    网络编程
  • 原文地址:https://www.cnblogs.com/holdon521/p/4599802.html
Copyright © 2011-2022 走看看