I know that theme was already actual and not just one time, but I can't find exactly what I need.
I have a function where I am making a call to another function. In that "another function" JS is changing source
of image. The problem is that the next statement runs before image's source is changed yet. I am using now a setTimeout
, but I want to make my code better and know how to deal with that.
function readURL(input) { //--preview of uploaded image--
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#image').attr('src', e.target.result);
$('.foto_add').css('background-position','center');
$('.foto_add').css('background-position-y','top');
$('.foto_add').css('background-image', 'url(' + e.target.result + ')');
}
reader.readAsDataURL(input.files[0]);
}
}
And then :
$("#avatar").change(function() {
readURL(this); //--calling of preview uploaded image function--
var $image = $('#image');
$image.cropper({
aspectRatio: 1,
autoCropArea: 0.1,
cropBoxResizable: false,
guides: false,
highlight: false,
toggleDragModeOnDblclick: false,
minCropBoxWidth: 200,
minCropBoxHeight: 200,
minCanvasHeight: 200,
zoomable: true,
viewMode: 1,//can't enter at empty canvas
dragMode: 'move',
responsive: true,
imageSmoothingEnabled: false,
imageSmoothingQuality: 'high',
ready: function () {
$image.cropper('setCanvasData', canvasData);
$image.cropper('setCropBoxData', cropBoxData);
}
});