在调用每个函数后执行代码

时间:2022-11-30 01:46:26

I have a scrolltobottom() function that is run after every function is executed.

我有一个scrolltobottom()函数,在执行每个函数后运行。

How can I refactor the code so that I don't need to type scrolltobottom() everywhere?

我怎样才能重构代码,这样我就不需要在任何地方输入scrolltobottom()?

Preferably without using Jquery.

最好不要使用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()
}

1 个解决方案

#1


4  

You could create a decorator function

您可以创建装饰器功能

function decorateWith(targetFn, fn){
  return function(){
    targetFn.apply(this, arguments);
    fn();
  }
}

[ botui.message.bot,
  botui.message.button ]
  .forEach(fn => decorateWith(fn, scrolltobottom));

#1


4  

You could create a decorator function

您可以创建装饰器功能

function decorateWith(targetFn, fn){
  return function(){
    targetFn.apply(this, arguments);
    fn();
  }
}

[ botui.message.bot,
  botui.message.button ]
  .forEach(fn => decorateWith(fn, scrolltobottom));