0

I want that the second function(lastfunction) inside foreach function will get called after 1 minute during each loop. i have tried different ways but nothing working out. its a javascript page. how to handle it?

if (t == '/routing') {
    _noSide();
    $('.sending').unbind("click").click(function() {


        var first = $("#first").val();
        var second = $("#second").val();
        var data = {
            'first': first,
        };

        $.post(apiUrl + "abc.php?nextfunction", data, function(res) {
            if (res.code == 200) {
                $("#myModal-100").modal('hide');
                if ((res.users).length) {
                    (res.users).forEach(function(vl, i) {

                        var to = vl.add;
                        console.log(to);
                        console.log(amount);
                        var dataa = {
                            'to': to,
                            'second': second,
                        };

                        setTimeout($.post(apiUrl + "abc.php?lastfunction", dataa, function(res) {
                            if (res.code == 200) {
                                notification(res.status);
                            } else {
                                notification(res.status);
                            }
                        });, 6000);

                    });

                }
            }
        });
    });

}
Satpal
  • 129,808
  • 12
  • 152
  • 166
Suman Raj
  • 1
  • 2
  • 1
    60 seconds will be 60000ms not 6000 – ellipsis Jan 03 '19 at 06:41
  • yes. but still it is not working – Suman Raj Jan 03 '19 at 06:42
  • 2
    Wrap your code in anonymous function i.e. `setTimeout(function(){//Your Code }, 60000)` – Satpal Jan 03 '19 at 06:42
  • first there is problem with your setTimout implementation. setTimeout first take a function and then takes duration in miliseconds. you need to wrap you $.post call of setTimeout in function – Saad Mehmood Jan 03 '19 at 06:42
  • `$.post` won't return the function reference `setTimeout` is expecting. Please read the [docs](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout). – Teemu Jan 03 '19 at 06:42
  • @SaadMehmood how to use it with $.post?? – Suman Raj Jan 03 '19 at 06:47
  • @SumanRaj just as @Satpal mentioned ```setTimeout(function(){//just paste you $.post code here }, 60000)```. – Saad Mehmood Jan 03 '19 at 06:49
  • @Satpal will the function automatically called then? – Suman Raj Jan 03 '19 at 06:51
  • @SumanRaj yes. setTimout will call function after 1 minute – Saad Mehmood Jan 03 '19 at 06:52
  • @Satpal it is working but only 1 time. for next value of "i" in foreach loop , it is not waiting – Suman Raj Jan 03 '19 at 07:03
  • You might have a problem with closure inside loops: https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – Armel Jan 03 '19 at 07:05
  • setTimeout function is running only once and i have n number of values in i. so for only first value of "i" it is running. else it is not running. please provide other solution – Suman Raj Jan 04 '19 at 03:59
  • @Satpal setTimeout function is running only once and i have n number of values in i. so for only first value of "i" it is running. else it is not running. please provide other solution – Suman Raj Jan 04 '19 at 06:33
  • @Satpal appreciate your effort but its not working.... – Suman Raj Jan 04 '19 at 08:13

0 Answers0