Guys, do not really understand how to rewrite my function to an asynchronous function?
I read a few articles on Habré, but apparently not very carefully, or I just do not understand how to do it.
I understand the principle of asynchrony and I understand the essence, but I do not understand how to apply it to my code.
I need my ajax request to be executed after calculating all the mathematical(non-complex) calculations.
=function code(id, coord, ap_zp_id, ap, appl_w, appl_h) {

        var str=coord;
        var arr=str.split('');//make an array
        //get the width of the coordinates
        width_c=arr[2];
        //get left margin from coordinates
        left_c=arr[0];
        //get the indent from the top of the coordinates
        top_c=arr[1];
        //get the length from the top of the coordinates
        height_c=arr[3];
        percent_origin=(192 * 100)/502;

        /* we consider new block sizes where to insert a picture * /
            new_width_c=(width_c * percent_origin)/100;
            new_width_calc=new_width_c;
            new_left_c=(left_c * percent_origin)/100;
            new_top_c=(top_c * percent_origin)/100;
            new_top_calc=new_top_c;
            new_height_c=(height_c * percent_origin)/100;
            new_height_calc=new_height_c;

        p_h_new=appl_h/(appl_w/new_width_c);
        if(p_h_new>new_height_c) {
            new_width_c=appl_w/(appl_h/new_height_c);
            new_left_c=new_left_c +(new_width_calc - new_width_c)/2;
            new_top_c=new_top_c +(new_height_calc - p_h_new)/2;
            if(new_top_c<new_top_calc) {new_top_c=new_top_calc};
        }

        $('.image_customs _'+ ap_zp_id) .css({'left': new_left_c +'px','top': new_top_c +'px','max-height': new_height_c +'px','max-width': new_width_c +'px'});

        var n_new_top_c=((new_height_c - $('.image_customs _'+ ap_zp_id) .height())/2) + new_top_c;
        $('.image_customs _'+ ap_zp_id) .css('top', n_new_top_c +'px');
        $('.a _'+ ap) .removeClass('hidden');

            $.ajax({
                type:"POST",
                url:"/ajax/add_style_print.php",
                data: {
                    str:'position: absolute;left:'+ new_left_c +'px;max-height:'+ new_height_c +'px;max-width:'+ new_width_c +'px;top:'+ n_new_top_c +'px;',
                    id: id,
                }
            }).done(function(datarez) {
               console.log('Added to the database');
            });

    }
  • Now this is exactly what should happen if you correct the syntax error. – Heroic Hippo Apr 5 '18 at 10:13
  • Heroic Hippo, which if not a secret? – Enchanting Elk Apr 5 '18 at 10:20
  • [[Kirill-Gorelov]], closer to ajax vyhovu slash superfluous at the end of the line – Heroic Hippo Apr 5 '18 at 11:06
  • Heroic Hippo, aaaaaa, no, it was there that I had a comment, I copied the code here, deleted the comment, and did not delete the first slash.
    Observant)
    – Enchanting Elk Apr 5 '18 at 11:22

1 Answers 1

In this example, the ajax request will be executed after all the calculations.
  • I absolutely agree, but sometimes there is some kind of delay in the calculations and ajax works faster than necessary (((
    And setting the delay to settimeout, as one of the examples of asynchrony, is not an option, I don’t know what the delay is. And even half a second a bit critical for the project ((
    – Enchanting Elk Apr 5 '18 at 10:19
  • [[Kirill-Gorelov]] cannot be – Heroic Hippo Apr 5 '18 at 11:05