Robin Alexander
Robin Alexander

Reputation: 1004

How to Restart a jQuery Ticker

I am successfully using BenjaminRH's jquery-ticker. It is fired after a prompt field submission but once the ticker ran once and needs to be fired again it doesn't.

The ticker is launched with

$('.ticker').ticker();

I tried to restart the ticker again with

$('.ticker').ticker().unbind();
$('.ticker').ticker();

Without success. Help is hugely valued. Thanks.

Upvotes: 1

Views: 137

Answers (1)

charlietfl
charlietfl

Reputation: 171698

The plugin is very lightweight and doesn't appear to have a restart option.

One approach would be store the ticker element html and use a function to start the ticker that replaces the html and initializes the plugin.

This function can then be called at any time to do a restart

Something like:

const tickHtml = $('.ticker').html();

function startTicker() {
  $('.ticker').html(tickHtml).ticker()
}
// start on page load
$(function() {
  startTicker()
})

// restart on click
$('button').click(function() {
  startTicker()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/jquery.ticker.min.js"></script>

<button>Start again</button>

<div class="ticker">
  <strong>News:</strong>
  <ul>
    <li>Ticker item #1</li>
    <li>Ticker item #2</li>
    <li><em>Another</em> ticker item</li>   
  </ul>
</div>

Upvotes: 2

Related Questions