小编Dav*_*ena的帖子

MutationObserver 仅用于最后一次突变

我正在使用MutationObserver保存可拖动对象中的位置更改。

它看起来像这样:

 let observer = new MutationObserver( (mutations) => {
        mutations.forEach( (mutation) => {
            this.builderData[element.id].$position.left = element.style.left;
            this.builderData[element.id].$position.top = element.style.top;
            this.saveBuilderData();
        });
    });
    observer.observe(element, { attributes : true, attributeFilter : ['style'] });
Run Code Online (Sandbox Code Playgroud)

然而,这个对每个像素的运行都改变了,所以运行了很多节省操作。我只想在它停止变异大约 1 秒后保存,或者每个回调都排除前一个。我已经用RxJava做过类似的事情,但没有用MutationObserver.

有任何想法吗?

javascript mutation-observers

2
推荐指数
1
解决办法
1621
查看次数

Vue文件上传——连续上传同一个文件

我正在尝试为聊天应用程序制作文件上传功能。我的 HTML 看起来像:(我不使用表单)

 <span class="file-attach mr3">
  <input id="uploadFile" name="uploadFile" type="file" @change="attachFile($event.target.files)" multiple>
  <i class="w-10 ml4 icon-attach"></i>
</span>
Run Code Online (Sandbox Code Playgroud)

我的打字稿文件看起来像:

 async attachFile(files) {
if (!files.length) {
  return
}
const reader = new FileReader()
reader.addEventListener('loadend', () => {
  this.mediaLinkDocument = {
    title: this.file.type.indexOf('image') !== -1 ? '' : this.file.name,
    type: this.file.type,
    size: this.file.size,
  }
  this.sendFile(this.file)
})

Array.from(Array(files.length).keys()).forEach((id) => {
  console.log(files[id])
  this.file = files[id]

  if (this.file.size > MAX_ATTACHMENT_SIZE) {
    alert('O tamanho máximo é de 20 MB')
    this.file = undefined

    return
  }
  reader.readAsDataURL(this.file)
  files = …
Run Code Online (Sandbox Code Playgroud)

file-upload typescript vue.js

2
推荐指数
1
解决办法
1566
查看次数