Muntashir Are Rahi Muntashir Are Rahi - 3 months ago 9
HTML Question

3 JS code blocks in a js file and never repeat a block if placed 3 times in same html page

I have 3 blocks of code lets call it

block-1
,
block-2
and
block-3
in the file
script.js
. I called
script.js
3 times in the same html page. I need to create condition of write function in such a way that each block will be executed just once. For example in the first call only
block-1
will be executed in the second call
block-2
and in third call
block-3
will be executed.

I have written the following condition. its working for 2 blocks but i neeed it for three.

if (typeof first === 'undefined') {
// FIRST BLOCK

var first = 0;
var second = 1;
}

if (second == 2) {

// SECOND BLOCK

}
second = 2;


How Can i do that?

Answer
var i=0;
function callthis(){
i++;
if(i==1){
//first
}
if(i==2){
//second
}
if(i==3){
//third
}
}