-1

I have some javascript code which displays a form on loading the web page. I need the code to execute 2 minutes after the visitor has been on the website. How do I go about it? Below is my code:

setTimeout(function(){document.getElementById("dialog").innerHTML="";}, 120000);

$(document).ready(function() {  

        var id = '#dialog';

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(500); 
        $('#mask').fadeTo("slow",0.9);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000);     

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });     

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });     

});    

GoBusto
  • 4,632
  • 6
  • 28
  • 45

2 Answers2

0

Use window.setTimeout():

The setTimeout() method [...] sets a timer which executes a function or specified piece of code once the timer expires.

Syntax

var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]);
var timeoutID = scope.setTimeout(function[, delay]);
var timeoutID = scope.setTimeout(code[, delay]);

So in your case, you could do this:

// When the page first loads...
$(document).ready(function() {
  // ...wait for two minutes...
  window.setTimeout(function() {
    // ...and then run this code.
  }, 120000)
})

More details: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout

GoBusto
  • 4,632
  • 6
  • 28
  • 45
0

The code must be INSIDE the setTimeout(), not OUTSIDE. Try it...