How to merge, combine two different functions in javascript?

Hello, world! I need help on something.I am new in javascript and have two different scripts to merge, combine or whatever they call it:

function doSomething() {}

(function loop() {
var rand = Math.round(Math.random() * (30000 - 1000)) + 1000;
setTimeout(function() {
alert('A');
doSomething();
loop(); 
}, rand);
}());

And this one:

var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen…
if(pos=="center"){LeftPosition=(screen…
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20…
settings='width='+w+',height='+h+',top…
win=window.open(mypage,myname,settings…

Can someone help me on this matter?

Many thanks to everyone.

Hi there,

You should just be able to do this:

function doSomething(){}

function NewWindow(mypage,myname,w,h,scroll,pos){
  if(pos=="random"){
    LeftPosition=(screen…
  }
  if(pos=="center"){
    LeftPosition=(screen…
  } else if((pos!="center" && pos!="random") || pos==null){
    LeftPosition=0;
    TopPosition=20…
    settings='width='+w+',height='+h+',top…
    win=window.open(mypage,myname,settings…
}

function loop() {
  var rand = Math.round(Math.random() * (30000 - 1000)) + 1000;
  setTimeout(function() {
    alert('A');
    doSomething();
    loop(); 
  }, rand);
}

var win=null;
NewWindow();
loop();

If you are having a specific problem or have any further questions, a link to the page in hand might help.

I appreciate your help, Pullo, but there’s a syntax error on line 11 and I can’t fix it.Can you see the what is the problem?

The code you provided in your original post was incomplete (it was truncated).
Can you provide the full code, or better still a link to a page I can see it on.

Sorry about that.I haven’t seen it was like that. The two codes are:

code 1:

function doSomething() {}

(function loop() {
var rand = Math.round(Math.random() * (30000 - 1000)) + 1000;
setTimeout(function() {
alert(‘A’);
doSomething();
loop();
}, rand);
}());

code 2:

var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos==“random”){LeftPosition=(screen.width)?Math.floor(Math.random()(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()((screen.height-h)-75)):100;}
if(pos==“center”){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!=“center” && pos!=“random”) || pos==null){LeftPosition=0;TopPosition=20}
settings=‘width=’+w+‘,height=’+h+‘,top=’+TopPosition+‘,left=’+LeftPosition+‘,scrollbars=’+scroll+‘,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no’;
win=window.open(mypage,myname,settings);}

What I want is a popup that appears randomly at a certain time(not 30 seconds,of course) and at a certain point of the screen, when the page is loaded.This is for a game for my website.When the popup appears, the user will click on it and be redirected to another page.

I really, really appreciate your help.

OK, if this is your actual code, then this is an empty function and does nothing:

function doSomething() {}

This is also a complete function, but is never called:

function NewWindow(mypage,myname,w,h,scroll,pos){
  if(pos=="random"){
    LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
    TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
  }
  if(pos=="center"){
    LeftPosition=(screen.width)?(screen.width-w)/2:100;
    TopPosition=(screen.height)?(screen.height-h)/2:100;
  } else if((pos!="center" && pos!="random") || pos==null){
    LeftPosition=0;
    TopPosition=20
  }
  settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
  win=window.open(mypage,myname,settings);
}

This is an immediately invoked function expression which executes as soon as the script is loaded:

(function loop() {
  var rand = Math.round(Math.random() * (30000 - 1000)) + 1000;
  setTimeout(function() {
    alert('A');
    doSomething();
    loop(); 
  }, rand);
}());
var win=null;

this is a global variable.

So, regardless of how you organize this code, only the IIFE will get called.

What I want is a popup that appears randomly at a certain time(not 30 seconds,of course) and at a certain point of the screen, when the page is loaded.

Almost all modern browsers block popups, so you might want to debate the wisdom of this technique.

When the popup appears, the user will click on it and be redirected to another page.

So you want the popup window to close and the main window to redirect. Is that correct?

That is correct.There’ll be a warning for the user to disable the popups blocker to play the game.

In that case, do this:

var rand = Math.floor((Math.random()*10)+1)*1000;

function newWindow(mypage,myname,w,h,scroll,pos){
  console.log("hi");
  if(pos=="random"){
    LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
    TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
  }
  if(pos=="center"){
    LeftPosition=(screen.width)?(screen.width-w)/2:100;
    TopPosition=(screen.height)?(screen.height-h)/2:100;
  } else if((pos!="center" && pos!="random") || pos==null){
    LeftPosition=0;
    TopPosition=20
  }
  settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
  win=window.open(mypage,myname,settings);
}

setTimeout(function() {
  newWindow('test.html', 'An annoying popup', 250, 100, 'no', 'random');
}, rand)

“An annoying popup”, that was very good :smiley:
Thank you so very much.That worked fine.It took me days, trying to figure out how to do it, watching tutorials and stuff, but wasn’t an easy thing to do.I appreciate the time you spent helping me out.

Best regards.