Add timeout function to reveal modal with cookie

1.6k Views Asked by At

I've successfully used this script to set a jquery cookie that shows site visitors a reveal modal but only once per day.

<script type="text/javascript">
$(document).ready(function(){
        // if the cookie doesn't exist create it and show the modal
        if ( ! $.cookie('hereToday') ) {

        // create the cookie. Set it to expire in 1 day
        $.cookie('hereToday', true, { expires: 1 });

        //call the reveal modal
        $('#subscribe').reveal();
    }
});
</script>

How can I add a timeout function to the script that would add a few seconds delay before firing the modal?

2

There are 2 best solutions below

0
On BEST ANSWER

you must use, setTimeout function:

<script type="text/javascript">
$(document).ready(function(){
        // if the cookie doesn't exist create it and show the modal
        if ( ! $.cookie('hereToday') ) {

        // create the cookie. Set it to expire in 1 day
        $.cookie('hereToday', true, { expires: 1 });

        //call the reveal modal
        var delay=5000; //in ms, this would mean 5 seconds
        setTimeout(function(){
            $('#subscribe').reveal();
        },delay);
    }
});
</script>
0
On

Just use setTimeout.

setTimeout(function() {
    $('#subscribe').reveal();
},5000);

Modal will be called after 5 seconds.