javascript - 在流畅的 Javascript API 中延迟函数的返回

标签 javascript

我正在构建一个小模态包,我想要一个流畅的 api,我想做的一件事是能够选择将动画函数传递到 close 方法中并调用该方法仅在可选动画完成后运行。

a) 我的想法正确吗?
b) 我应该只使用回调吗?
c) 有没有使用 setTimeout 的替代方法? (我不知道为什么这感觉很脏,但确实如此) d) 我认为 Promise 是一个选项,但这会破坏语法。

我想要得到的是:

   Modal.save().close().resetData();

最佳答案

a) I'm thinking about this correctly?

是的。

b) Should I just be using a callback?

是的,这种方法没有问题。

c) is there an alternative to using a setTimeout? (I don't know why this feels dirty but it does)

对于动画,请使用 CSS 过渡或 requestAnimationFrame 而不是 setTimeout

d) I suppose promises are an options but that would disrupt the syntax.

当你的 promise 起作用时,你可以重构它。 Promise 确实提供了更流畅的 API。

使用回调方法,我希望它看起来像这样:

function close(done) {
  // do something async then call `done`
}

Modal.save(close.bind(Modal, resetData));

关于javascript - 在流畅的 Javascript API 中延迟函数的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121137/

相关文章:

javascript - 我什么时候应该使用 `publishReplay` 与 `shareReplay` ?

javascript - 通过 Node.js 将文档插入 MongoDB 时出错

javascript - 访问 jquery.each 中的元素 id 属性值

jquery - 如何创建具有特殊行为的元素

JavaScript:私有(private)成员被另一个实例覆盖

javascript - 如何在来自不同网站集的 SharePoint 2010 页面的模式对话框中打开新列表项表单

javascript - 如何在 socket.io 事件后重定向客户端?

javascript - 如何正确销毁CKEditor实例?

javascript - AngularJS .open Modal() 返回错误而不是函数

javascript - 当img标签进入js对象时如何从javascript中的图像中提取src