0

I'm generating HTML table using JavaScript objects, I created two functions with different headers:

function generateTable(data) {
let newobj = data.filter(function(el) {
        return el.Sens == 'Depart';
    })
    .map(function(obj) {
        return {
            Heure: moment(obj.prevu).format("h:mm"),
            Vol: obj.Numvol,
            Compagnie: 'uploads/' + obj.FileName,
            Destination: obj.AeroD + '  via  ' + obj.AeroA,
            Comptoir: obj.Comptoir.replace(/[\[\]']+/g, ''),
            Porte: obj.porte,
            Infos: obj.etat,
            Enregistrement: obj.active_enregistrement,
        }
    });}

Second Function :

function generateArTable(data) {
let newobj = data.filter(function(el) {
        return el.Sens == 'Depart';
    })
    .map(function(obj) {

        return {
            'التوقيت': moment(obj.prevu).format("h:mm"),
            'الرحلة': obj.Numvol,
            'الناقل الجوي': 'uploads/' + obj.FileName,
            'الوجهة': obj.AeroD_ar + ' عبر ' + obj.AeroA_ar,
            'الشباك': obj.Comptoir.replace(/[\[\]']+/g, ''),
            'الباب': obj.porte,
            'معلومات': obj.etat

        }
    });

they both generate an HTML table using the same table

let table = document.querySelector("#fr_table");

I want to a create function which has two parameters N and X:

  • N is how many times to execute two functions
  • X is interval between execution of the two functions

I have tried these functions:

 setTimer0 = setInterval(function() {
    generateArTable(data);
    console.log("first function executed");

}, 3500, (0));

setTimer1 = setInterval(function() {
    generateTable(data);
    console.log("second function executed");

}, 3100, (1));

but it's not working as expected for the interval between the two functions.

1 Answer 1

1

N is how many times to execute two functions X is interval between exection of the two functions:

function f1(){console.log(1);}
function f2(){console.log(2);}

function asYouWish(N, X) {
  while(N--){
    f1();
    setTimeout(f2, X);
  }
}
asYouWish(3, 500);

N is how many times to execute two functions X is interval between exection of each next function:

function f1(){console.log(1);}
function f2(){console.log(2);}

function asIThinkYouReallyWish(N, X) {
  for(let i = 0; i < N*2; i++) {
    setTimeout(i%2? f2 : f1, i * X);
  }
}
asIThinkYouReallyWish(3, 500);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that's exactly what i needed :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.