web3.eth.isSyncing
该函数将在每次同步开始、更新和停止时调用指定的回调函数。
调用方法:
web3.eth.isSyncing(callback);
返回值:
返回对象syncing
具有如下方法:
- syncing.addCallback(): 添加另一个回调函数, 当节点开始或停止同步时将被调用
- syncing.stopWatching(): 停止调用回调函数
回调函数的返回值:
- Boolean - 当同步开始时返回true,停止时返回false
- Object - 在同步过程中将返回
syncing
对象,有以下字段:- startingBlock: Number - 同步开始时的块号
- currentBlock: Number - 当前已经同步的最后块号
- highestBlock: Number - 预估需要同步的块号
示例代码:
web3.eth.isSyncing(function(error, sync){
if(!error) {
// stop all app activity
if(sync === true) {
// we use `true`, so it stops all filters, but not the web3.eth.syncing polling
web3.reset(true);
// show sync info
} else if(sync) {
console.log(sync.currentBlock);
// re-gain app operation
} else {
// run your app init function...
}
}
});