Add progress bar and auto2top (#27)
* Add progress bar * Add abort ajax * Remove console * Add cache folder * Add auto2top
This commit is contained in:
parent
ec53f1e307
commit
2610e25040
11 changed files with 100 additions and 16 deletions
12
src/util.js
12
src/util.js
|
|
@ -1,10 +1,11 @@
|
|||
/**
|
||||
* Simple ajax
|
||||
* @param {String} url
|
||||
* @param {String} [method=get]
|
||||
* @param {String} [method=GET]
|
||||
* @param {Function} [loading] handle loading
|
||||
* @return {Promise}
|
||||
*/
|
||||
export function load (url, method = 'get') {
|
||||
export function load (url, method = 'GET', loading) {
|
||||
const xhr = new XMLHttpRequest()
|
||||
|
||||
xhr.open(method, url)
|
||||
|
|
@ -12,11 +13,16 @@ export function load (url, method = 'get') {
|
|||
|
||||
return {
|
||||
then: function (success, error = function () {}) {
|
||||
if (loading) {
|
||||
xhr.addEventListener('progress', loading)
|
||||
xhr.addEventListener('loaded', loading)
|
||||
}
|
||||
xhr.addEventListener('error', error)
|
||||
xhr.addEventListener('load', ({ target }) => {
|
||||
target.status >= 400 ? error(target) : success(target.response)
|
||||
})
|
||||
}
|
||||
},
|
||||
abort: () => xhr.readyState !== 4 && xhr.abort()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue