将一个字符串翻转,例 '123456' -> '654321'
// 翻转字符串算法
function fanzhuan( str ) {
let result = ''
for ( let i = str.length; i > 0; i-- ) {
result += str.slice( i-1, i )
}
return result
}
let str = '123456'
let str2 = fanzhuan( str ) // 654321
let str3 = fanzhuan( '123456789abc' ) //cba987654321
let str4 = fanzhuan( '清欢' ) // 欢清
有a,b,c...等十个js文件,需要按照顺序引入,使用promise封装
// 封装 promise
function promiseOnloadJS ( url ) {
return new Promise( ( resolve, reject )=> {
const node = document.createElement('script')
node.type = 'text/javascript'
node.src = url
node.onload = function() { // 在js 文件加载完成 ,调用resolve
resolve()
}
document.head.appendChild(node)
})
}
promiseOnloadJS('js/temp/a.js').then(function() {
console.log('---------------');
return promiseOnloadJS('js/temp/b.js')
}).then(function() {
console.log('---------------');
return promiseOnloadJS('js/temp/c.js')
}).then(function() {
console.log('---------------');
return promiseOnloadJS('js/temp/d.js')
}).then(function() {
console.log('---------------');
return promiseOnloadJS('js/temp/e.js')
}).then(function() {
console.log('---------------');
return promiseOnloadJS('js/temp/f.js')
}).then(function() {
console.log('---------------');
return promiseOnloadJS('js/temp/g.js')
}).then(function() {
console.log('---------------');
return promiseOnloadJS('js/temp/h.js')
}).then(function() {
console.log('---------------');
return promiseOnloadJS('js/temp/i.js')
})