Javascript 在调用每个函数后执行代码
我有一个Javascript 在调用每个函数后执行代码,javascript,vue.js,Javascript,Vue.js,我有一个scrolltobottom()函数,它在每个函数执行后运行 如何重构代码,使我不必到处键入scrolltobottom() 最好不使用Jquery async function initBot () { let botui = BotUI('homepage-bot', { vue: Vue }) let scrolltobottom = () => { let ele = document.getElementById('botui') d
scrolltobottom()
函数,它在每个函数执行后运行
如何重构代码,使我不必到处键入scrolltobottom()
最好不使用Jquery
async function initBot () {
let botui = BotUI('homepage-bot', {
vue: Vue
})
let scrolltobottom = () => {
let ele = document.getElementById('botui')
document.body.scrollTop = ele.clientHeight
}
await botui.message.bot({"Hello"})
await scrolltobottom()
await botui.message.bot({"Do you like apple or orange?"})
await scrolltobottom()
await botui.message.button({"Orange", "Apple"})
await scrolltobottom()
}
您可以创建一个decorator函数
function decorateWith(targetFn, fn){
return function(){
targetFn.apply(this, arguments);
fn();
}
}
[ botui.message.bot,
botui.message.button ]
.forEach(fn => decorateWith(fn, scrolltobottom));
如果所有其他函数都是
botui.message.bot
,而不是botui.message.bot
和botui.message.button
,那么您只需将滚动框放到botui.message.bot
中,为什么要等待一个同步函数?