我想这可能很明显,但我在前端方面很菜鸟。
场景如下:每当用户点击一个按钮时,我都需要
1-通过块UI
阻止页面2-清除一些东西
3-加载一些东西
4-取消阻止页面
让我们有一个片段,类似于我目前的方法:
function reload_data()
{
//block
$.blockUI({ message: $('#spinner_div') , css: { top: '50%' , left:'50%' , border:'none'} });
//clear stuff
$('#my_kpi' ).text(''); //this empty an anchor
$('#my_report iframe').attr('src',''); //this empty an iframe
//load stuff
$('#my_kpi' ).text('loaded'); //this sets an anchor
$('#inf_prioridad_estado iframe').attr('src','foo.php'); //this loads an iframe
//unblock
setTimeout(function(){ alert("I would like not to unblock until iframes are loaded");},2000);
setTimeout($.unblockUI, 2000); //will be blocked unless for 2 seconds
console.log('unblocked');
}
我知道(这么认为...发生了什么,但我不知道如何解决:
$((.attr('src','foo.php'(;确实执行,但在实际加载 iframe 之前继续执行下一条语句。
有什么想法吗?
非常感谢!
设置 iframe 的 src
属性后,需要侦听 load 事件:
// Set number of frames to be loaded
// This could be done with a jquery selector getting iframe count
var framesToLoad = 2;
function tryUnblockUI() {
// Frame loaded, decrement framesToLoad count
framesToLoad--;
// Once all frames are loaded, unblock UI
if (frameLoadingCount === 0) {
//unblock ui here
}
}
// Load frame 1
$('#inf_prioridad_estado iframe')
.attr('src', 'foo.php')
.on('load', tryUnblockUI);
// Load frame 2
$('#inf_prioridad_estado_2 iframe')
.attr('src', 'foo.php')
.on('load', tryUnblockUI);