关于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>