SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    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.
    Last edited by Pullo; Sep 1, 2013 at 02:52. Reason: Added code tags

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    You should just be able to do this:

    Code JavaScript:
    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.

  3. #3
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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.

  5. #5
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    OK, if this is your actual code, then this is an empty function and does nothing:

    Code JavaScript:
    function doSomething() {}

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

    Code JavaScript:
    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:

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

    Code JavaScript:
    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?

  7. #7
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is correct.There'll be a warning for the user to disable the popups blocker to play the game.

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    In that case, do this:

    Code JavaScript:
    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)

  9. #9
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "An annoying popup", that was very good
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •