The purpose of asynchronous loading is to improve the rendering performance of web pages in some scenarios.
Common synchronous loading code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript"
src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=Your%20key"></script>
<style>
html,
body,
#map {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = new BMapGL.Map('map');
var point = new BMapGL.Point(116.404, 39.915);
map.centerAndZoom(point, 10);
map.enableScrollWheelZoom(true);
</script>
</body>
</html>
Asynchronous loading method:
idea is to avoid initialization loaded directly, but put the initialization process in the init () function (), for other contents page finished loading, add script tags and callback function calls the init function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html,
body,
#map {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function init(){
var map = new BMapGL.Map('map');
var point = new BMapGL.Point(116.404, 39.915);
map.centerAndZoom(point, 10);
map.enableScrollWheelZoom(true);
}
window.onload = function(){
var script = document.createElement('script')
script.src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=Your%20key&callback=init"
document.head.appendChild(script)
}
</script>
</body>
</html>
Read More:
- Solution to some map files in JS folder after Vue packaging (remove the map. JS file)
- Solve the asynchronous execution of callback function in Axios request processing interceptor
- Method to solve uncaught typeerror: cannot set property ‘onclick’ of null error
- [Solved] devTools failed to load sourcemap: Could not parse content for map
- [Chrome]: DevTools failed to load source map… (How to Solve)
- Echarts Map Error: Error: Invalid geoJson format Cannot read property ‘length‘ of undefined
- Method of adding operation button for each line of data in DataGrid of easyUI
- Cesium.js:1 Error loading image for billboard: [object Event]
- How to Solve Vue loading 3D model Error
- [Solved] Syntax Error: Error: Loading PostCSS Plugin failed: Cannot find module ‘autoprefixer‘
- How to Fix “HTTP 405 method not allowed” Error
- Vue a page is mounted to send multiple requests at the same time, and the loading is processed uniformly
- Failed to resolve async component default: ChunkLoadError: Loading chunk 7 failed
- [Solved] JQuery each Method Error: $XXX is not defined
- The solution of calling$. Ajax successfully but the success method does not respond
- Solving routing errors by rewriting Vue push method
- Method of identifying vite alias path by idea webstorm
- [Solved] ERROR Error loading vue.config.js: ERROR Error: Command failed: git describe
- [Solved] Element form method Error: TypeError: Cannot read properties of undefined (reading ‘resetFields’)
- [Solved] Element form method resetfields() error: vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read properties of undefined (reading ‘indexOf’)