JustMe
JustMe

Reputation: 327

Prevent a page loading after click for a specific time

I want to display a jQuery dialog box for a specific period of time before the clicked link begins loading the page but I'm having an issue with the duration. It doesn't seem to last as I expected. How can I fix? Thanks!

$(function() {
            $( "#dialog" ).dialog({
               autoOpen: false,  
            });
            $(".images").find("a").eq(1).on('click', function(e) {
                setTimeout(() => {
                    e.preventDefault();
                }, 10000);
                $( "#dialog" ).dialog( "open" );
            });
        });
#dialog {
        display: none;
    }
    
img {
        width: 300px;
        height: 250px;
        object-fit: cover;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <script src=""></script>
    <link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
    
    <div class="images">
        <a href="https://www.site1.com">
            <img src="https://images.pexels.com/photos/413727/pexels-photo-413727.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="Image 1">
        </a>
        <br>
        <a href="https://www.site2.com">
            <img src="https://images.pexels.com/photos/1036623/pexels-photo-1036623.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="">
        </a>
    </div>
    <div id="dialog" title="Lorem ipsum">
        <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quod amet, et minus commodi repellendus hic? Ut eos blanditiis quis provident.</p>
    </div>

Upvotes: 0

Views: 41

Answers (1)

ikiK
ikiK

Reputation: 6532

You can not put e.preventDefault() inside timeout, you need it right after click so that browser does not navigate away. And inside your timeout set the redirection click:

$("#dialog").dialog({
  autoOpen: false,
});

$(".images").find("a").eq(1).on('click', function(e) {
  e.preventDefault();
  setTimeout(() => {
    window.location.href = $(this).prop("href")
  }, 3000);
  $("#dialog").dialog("open");
});
#dialog {
  display: none;
}

img {
  width: 300px;
  height: 250px;
  object-fit: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src=""></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />

<div class="images">
  <a href="https://www.site1.com">
    <img src="https://images.pexels.com/photos/413727/pexels-photo-413727.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="Image 1">
  </a>
  <br>
  <a href="https://www.site2.com">
    <img src="https://images.pexels.com/photos/1036623/pexels-photo-1036623.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="">
  </a>
</div>
<div id="dialog" title="Lorem ipsum">
  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quod amet, et minus commodi repellendus hic? Ut eos blanditiis quis provident.</p>
</div>

Upvotes: 1

Related Questions