确定何时完成 JavaScript Dropbox 数据存储 API 的插入

Determining When the JavaScript Dropbox Datastore API is Done Inserting

本文关键字:存储 API 插入 数据 Dropbox 何时完 JavaScript      更新时间:2023-09-26

我正在使用JavaScript API将大量记录(2000-3000)导入到Dropbox数据存储中。我试图阻止 UI,直到导入完成,因为如果用户离开页面,导入将终止部分。

这是我的代码,其中包含对正在发生的事情的注释:

//This shows a "loading" spinner
showLoader('Standby','Importing logbook...');
//Fire import function
writeToDropbox('aircraft', data.aircraft);
function writeToDropbox(tableName, dataArray){
  //Write up to 100 items per delta
  for(var i = 0; i < dataArray.length && i < 100; i++) {
    //Write to Dropbox
    datastore.getTable(tableName).insert(dataArray[i]);
  }
  if(i < dataArray.length) { 
    //More to write
    window.setTimeout(function(){
      //After a tick, continue from where we left off              
      writeToDropbox(tableName, dataArray.slice(i));      
    }, 1);
  }
  //Check progress
  if(dataArray.length == 0){
    //Writing complete... show the confirmation modal
    hideLoader();
    $('#modal-done').show(); //(!) This happens before all the records are inserted
  }  
}

我遇到的问题是 JavaScript 全部执行并且我的应用程序显示确认模式,但在后台数据存储 API 仍在插入记录。

如何可靠地确定 API 何时完成插入记录?

请参阅 https://www.dropbox.com/developers/blog/61/checking-the-datastore-sync-status-in-javascript。

检查datastore.getSyncStatus().uploading