Flash
Flash

Reputation: 45

Jquery time between clicks

Can anybody tell me where is the problem here, I want when i click #automatic to start opening new windows with 3 sec delay

$('#automatic').click(function(){           
     $('.autosend').each(function() {
    //window.open( $(this).attr('href') );
    var openwindow = window.open( $(this).attr('href') );
    setTimeout(openwindow,3000);
    });         
            });

Upvotes: 1

Views: 43

Answers (1)

gaetanoM
gaetanoM

Reputation: 42044

If you need to open new window with a delay of 3 seconds this line is wrong:

setTimeout(openwindow,3000);

$(function () {
  $('#automatic').click(function(){
    $('.autosend').each(function(index, element) {
      $('#txt').text($('#txt').text() + '\n' + (index * 3000));
      setTimeout(function() {
        var openwindow = window.open( $(this).attr('href') );
        $('#txt').text($('#txt').text() + '\n' + 'Window open: ' + $(this).attr('href'));
      }.bind(this), index * 3000);
    });
  });
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<button id="automatic">Automatic</button>
<a href="1.html" class="autosend"></a>
<a href="2.html" class="autosend"></a>
<a href="3.html" class="autosend"></a>
<a href="4.html" class="autosend"></a>
<textarea id="txt" style="height: 200px"></textarea>

Upvotes: 1

Related Questions