Created
February 29, 2020 13:23
-
-
Save kokizzu/2ba2d8431a8081af9a6862467603184f to your computer and use it in GitHub Desktop.
Global JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toDate( unix ) { | |
if( !unix ) return ''; | |
if( unix<9876543210 ) unix *= 1000; | |
return (new Date( unix )).toISOString().substr( 0, 10 ); | |
} | |
function toTime( unix ) { | |
if( !unix ) return ''; | |
if( unix<9876543210 ) unix *= 1000; | |
return (new Date( unix )).toISOString().substr( 0, 19 ).replace( 'T', ' ' ); | |
} | |
function currDate() { | |
return (new Date()).toISOString().substr( 0, 10 ); | |
} | |
function currTime() { | |
return (new Date()).toISOString().substr( 0, 19 ).replace( 'T', ' ' ); | |
} | |
function isFunc( f ) { | |
return 'function'== typeof (f); | |
} | |
function hasAjaxError( res ) { | |
if( !res ) return 'Server response is empty'; | |
if( res.error ) return res.error; | |
return ''; | |
} | |
function postAjax( url, data, successF, errorF, completeF ) { | |
console.log( 'Ajax post', url, data ); | |
return $.ajax( { | |
type: "POST", | |
url: url, | |
data: data | |
} ).done( function( res, status, xhr ) { | |
console.log( 'Ajax success', url, data, res, status, xhr ); | |
if( isFunc( successF ) ) successF( res, status ); | |
} ).fail( function( xhr, status, err ) { | |
console.log( 'Ajax error', url, data, status, err, xhr ); | |
if( isFunc( errorF ) ) errorF( xhr.status + ' ' + xhr.responseText + ': ' + err ); | |
} ).always( function( res_xhr, status, xhr_err ) { | |
if( isFunc( completeF ) ) completeF( res_xhr, status, xhr_err ); | |
} ); | |
} | |
function uploadAjax( url, data, successF, errorF, completeF, progressF ) { | |
console.log( 'Ajax upload', url, data ); | |
return $.ajax( { | |
type: "POST", | |
url: url, | |
data: data, | |
async: true, | |
processData: false, | |
contentType: false, | |
xhr: function() { | |
var xhr = $.ajaxSettings.xhr(); | |
if( !progressF ) return xhr; | |
console.log( xhr ); | |
progressF( 1 ); | |
xhr.upload.addEventListener( 'progress', function( e ) { | |
var prog = 1 + (e.lengthComputable) ? e.loaded * 90 / e.total : 1; | |
progressF( Math.round( prog * 100 ) / 100 ); | |
} ); | |
xhr.addEventListener( 'progress', function( e ) { | |
var prog = 92 + (e.lengthComputable) ? e.loaded * 8 / e.total : 1; | |
progressF( Math.round( prog * 100 ) / 100 ); | |
} ); | |
return xhr; | |
} | |
} ).done( function( res, status, xhr ) { | |
console.log( 'Ajax success', url, data, res, status, xhr ); | |
if( progressF ) progressF( 100 ); | |
if( isFunc( successF ) ) successF( res, status ); | |
} ).fail( function( xhr, status, err ) { | |
console.log( 'Ajax error', url, data, status, err, xhr ); | |
if( isFunc( errorF ) ) errorF( xhr.status + ' ' + xhr.responseText + ': ' + err ); | |
} ).always( function( res_xhr, status, xhr_err ) { | |
if( isFunc( completeF ) ) completeF( res_xhr, status, xhr_err ); | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage example: