0

How to call Ajax success function variable into Other function, please have a look over the code.

var global_data;

    $.ajax({
        type: "post",
        url: "login.php",
        success: function (result) {
            console.log(result);
            global_data =   result;
            tick(global_data);

        }
    });

function tick(global_data) {
        $.toast({

        text: '30 days club',
        icon: '',
        loader: false,        
        loaderBg: '#9EC600',  
        allowToastClose: true,
        showHideTransition: 'fade'
        })
    initMap();
    console.log ("Jerry"+global_data);
    setTimeout(tick, random(20, 24) * 1000);
}

Here is the code, please have a look and after making all the changes still getting undefined in the console for function tick(global_data).

  • 1
    `success: function (result) { tick(result);` and later `function tick(global_data) {` – mplungjan Jul 25 '18 at 09:53
  • AJAX calls are asynchronous, so you need to use the callback pattern properly. See the duplicates I marked for guides on how to do that. You should also remove `async: false` as it's incredibly bad practice. – Rory McCrossan Jul 25 '18 at 09:54
  • pass value in function parameter – TarangP Jul 25 '18 at 09:54
  • @ mplungjan you mean to say i have to use function tick(global_data) inside the success:function(result). – Gaurav kakkar Jul 25 '18 at 10:03
  • You have `setTimeout(tick, random(20, 24) * 1000);` - that does not have global data passed. You seem to after all to DO need the global data to exist as a global var. So Nikos was not wrong after all. You now need to remove it from the function call: `success: function (result) { console.log(result); global_data = result; tick(); }` and then have `function tick() { if (global_data) { ...process... } setTimeout(tick, random(20, 24) * 1000); }` – mplungjan Jul 25 '18 at 11:52
  • Thanks @mplungjan it's working for me. – Gaurav kakkar Jul 25 '18 at 12:23
  • Feel free to delete the question – mplungjan Jul 25 '18 at 12:25

0 Answers0