问题描述
Adobe Animate Canvas生成内容,然后将该内容附加到iframe中。然后绘制画布,并在iframes文档中创建各种脚本变量。
我需要访问adobe动画的iframe完全加载时生成的变量“ stage”。
我现在要做的是-附加setTimeout,然后似乎可以访问了。 如果我不附加超时,则控制台将引发错误。
我要实现的目标-我想避免setTimeout并以更好的方式重构代码。
我尝试过jquery:
$element.on( 'load',function() {
// Gives error when accessing the stage property
$element[0].contentwindow.stage
// allows to access stage property
setTimeout(() => {
console.log($element[0].contentwindow.stage)
},1000)
});
解决方法
所以基本上我已经尝试了很多建议。
尝试:
$(element).load
$(element).on('load')
Other stuff...
对我有用的是这段代码:
var onFullLoad = function(iframe) {
return new Promise((resolve) => {
var wait = function() {
if (iframe && iframe.contentWindow && iframe.contentWindow.YourTargetedVariable) {
resolve(iframe);
} else {
window.requestAnimationFrame(wait);
}
};
wait();
})
};
然后您可以像这样继续下去:
onFullLoad(yourTargetIframe).then(function(){
//your code
});