我的 setAttribute
函数有问题。
这是我的代码
mind.SetRequest(rec_input.value);
mind.Reply();
element.setAttribute('text', mind.GetReply());
element.speak();
element.speak()
不等待 setAttribute
完成。
当setAttribute
完成时,是否有任何函数可以调用speak()
?
img.onload = function(){ 。 。 。 }
函数在加载 img
后调用,是否有类似的方法可以在 setAttribute
之后调用我的 speak
函数?
最佳答案
试试这个:
mind.SetRequest(rec_input.value);
mind.Reply();
element.setAttribute('text', mind.GetReply());
setTimeout(function () {
element.speak();
}, 0);
setAttribute()
是同步的,但它是基于 DOM 的方法。这意味着浏览器需要一些额外的处理器滴答来设置该属性。因此,请使用超时将代码执行(尝试获取该属性)移至下一个事件循环。
关于javascript setAttribute 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30133489/