使用超时来完成这项工作不是一个好主意,因为你不知道它将花费多少时间来完成。因此,可以向jsonheader添加回调,如下所示:
import jQuery from 'jquery-ajax';
import Atlas from "./Atlas";
export default class JSONHeader {
constructor(location, callback) {
loadAtlasStructure(location);
function loadAtlasStructure(location, callback) {
jQuery.ajax({
dataType: "json",
url: location,
async: true,
success: function (files) {
files.map((file) => {
jQuery.ajax({
dataType: "json",
url: location + file,
async: true,
success: function (data) {
console.log(data);
if (!window.IndexAtlas) {
window.IndexAtlas = new Atlas();
}
window.IndexAtlas.addSubAtlas(data);
console.log('JSONHeader::window.IndexAtlas', window.IndexAtlas);
callback();
}
});
})
}
})
}
}
}
在reactjs中,只需创建一个回调并在调用jsonheader时传递它:
import React, {Component} from 'react';
import BrowserRouter from "react-router-dom/es/BrowserRouter";
import Switch from "react-router-dom/es/Switch";
import Route from "react-router-dom/es/Route";
import Redirect from "react-router-dom/es/Redirect";
import ScenePage from "../ScenePage/index";
import CoverPage from "../CoverPage/index";
import {INDEX, SCENE_PAGE} from "../../constantRoutes";
import JSONHeader from "../../newModel14Junio/JSONHeader";
export default class App extends Component {
constructor(props) {
super(props);
const finishCallback = () => {
this.setState({isAtlasLoading: false});
};
const header = new JSONHeader('/atlas/json/', finishCallback);
this.state = {
isAtlasLoading: true
};
}
render() {
if (this.state.isAtlasLoading) {
return (<div>Atlas loading</div>);
}
return (
<BrowserRouter>
<div>
<Switch>
<Route exact path={INDEX} component={CoverPage}/>
<Route path={SCENE_PAGE} component={() => <ScenePage IndexAtlas={window.IndexAtlas}/>}/>
<Redirect from="*" to={INDEX}/>
</Switch>
</div>
</BrowserRouter>
);
}
}
我希望我能帮助你。