多次触发JS脚本

时间:2019-09-27 13:15:10

标签: javascript

我想在页面上多次显示倒计时,但不能多次触发。我想过有什么办法可以做到的,因为每个人都可以这样做吗?

https://jsfiddle.net/3a4m71vo/

 public class TouchDetect:MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
    {

     public void OnPointerEnter(PointerEventData eventData)
      {
        if (eventData.pointerEnter.gameObject.name == "redcircle2(Clone)" )
            {
                sound[1].source.clip = sound[1].clip;
                sound[1].source.loop = true;
                sound[1].source.Play();
            }
       }

    }

1 个答案:

答案 0 :(得分:1)

您面临的问题是您没有为HTML / JavaScript使用ID。相反,您传递的是html(div)的类。

我添加了两个HTML块,并为其指定了onetwo的ID。

因此:

function getTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));

  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
  };
}

function initializeClock(id, endtime) {
  var clock = document.querySelector(id);
  var daysSpan = clock.querySelector('.days');
  var hoursSpan = clock.querySelector('.hours');
  var minutesSpan = clock.querySelector('.minutes');
  var oneMinute = 60000;

  function updateClock() {
    var t = getTimeRemaining(endtime);

    daysSpan.innerHTML = t.days;
    hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
    minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);

    if (t.total < oneMinute) {
      clearInterval(timeinterval);
      clock.style.display = "none";
    }
  }

  updateClock();
  var timeinterval = setInterval(updateClock, 1000);
}


initializeClock('#one', 'September 29 2019 13:18:00');

initializeClock('#two', 'September 28 2019 13:10:00');
.countdown__counter {
  display: flex;
  margin: 0 0 15px;
}

.countdown__counter-item {
  display: flex;
  margin: 0 15px 0 0;
}
<div id="one" class="countdown__counter js-countdown">
  <div class="countdown__counter-item">
    <span class="days bond-countdown__time"></span>
    <div class="bond-countdown__small-text">D</div>
  </div>

  <div class="countdown__counter-item">
    <span class="hours countdown__time"></span>
    <div class="bond-countdown__small-text">H</div>
  </div>

  <div class="countdown__counter-item">
    <span class="minutes countdown__time"></span>
    <div class="countdown__small-text">M</div>
  </div>
</div>

<div class="countdown__counter js-countdown">
  <div class="countdown__counter-item">
    <span class="days bond-countdown__time"></span>
    <div class="bond-countdown__small-text">D</div>
  </div>

  <div class="countdown__counter-item">
    <span class="hours countdown__time"></span>
    <div class="bond-countdown__small-text">H</div>
  </div>

  <div class="countdown__counter-item">
    <span class="minutes countdown__time"></span>
    <div class="countdown__small-text">M</div>
  </div>
</div>


<div id="two" class="countdown__counter js-countdown">
  <div class="countdown__counter-item">
    <span class="days bond-countdown__time"></span>
    <div class="bond-countdown__small-text">D</div>
  </div>

  <div class="countdown__counter-item">
    <span class="hours countdown__time"></span>
    <div class="bond-countdown__small-text">H</div>
  </div>

  <div class="countdown__counter-item">
    <span class="minutes countdown__time"></span>
    <div class="countdown__small-text">M</div>
  </div>
</div>

<div class="countdown__counter js-countdown">
  <div class="countdown__counter-item">
    <span class="days bond-countdown__time"></span>
    <div class="bond-countdown__small-text">D</div>
  </div>

  <div class="countdown__counter-item">
    <span class="hours countdown__time"></span>
    <div class="bond-countdown__small-text">H</div>
  </div>

  <div class="countdown__counter-item">
    <span class="minutes countdown__time"></span>
    <div class="countdown__small-text">M</div>
  </div>
</div>