web3.eth.isSyncing

优质
小牛编辑
130浏览
2023-12-01

web3.eth.isSyncing(callback)

提供同步开始,更新,停止的回调函数方法。

返回值:

Object - 一个syncing对象,有下述方法:

  • syncing.addCallback(): 增加另一个回调函数,在节点开始或停止调用时进行调用。
  • syncing.stopWatching(): 停止同步回调。

回调返回值:

  • Boolean - 同步开始时,此值为true,同步停止时此回调值为false
  • Object - 当正在同步时,会返回同步对象。
    • startingBlockNumber - 同步开始区块号
    • currentBlock: Number - 节点当前正在同步的区块号
    • highestBlock: Number - 预估要同步到的区块

示例:

//初始化基本对象
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var BigNumber = require('bignumber.js');


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...
        }
    }
});