JS实现商城秒杀倒计时功能(动态设置秒杀时间)

yizhihongxing

这里给出一个详细讲解JS实现商城秒杀倒计时功能(动态设置秒杀时间)的完整攻略,包含以下几个步骤:

步骤一:HTML结构

首先,在HTML页面中设置一个用来显示秒杀倒计时的元素,比如一个id为countdown<div>,这个元素用来显示剩余的天、时、分、秒。同时,还需要设置一个用来存储当前秒杀的时间戳的隐藏<input>元素,比如一个id为timestamp的隐藏<input>

下面展示示例的HTML结构:

<div id="countdown">
  <span id="days">00</span>
  <span id="hours">00</span>
  <span id="minutes">00</span>
  <span id="seconds">00</span>
</div>
<input type="hidden" id="timestamp" value="1598323200">

步骤二:获取并设置秒杀时间戳

接下来,在JS代码中,我们需要获取并设置当前秒杀的时间戳。这个时间戳可以通过后端接口获取,也可以直接在前端设置一个默认的值,比如下面这个示例的时间戳为2020年8月25日00:00:00对应的时间戳,即1598323200

示例代码:

const timestamp = document.getElementById('timestamp').value;

步骤三:倒计时计算与显示

在获取当前秒杀时间戳之后,我们需要根据当前时间和秒杀时间戳计算出剩余的天、时、分、秒,用来显示在页面中。

具体的计算方法如下:

// 倒计时计算
function countDown() {
  const now = new Date().getTime(); // 当前时间戳
  const diff = timestamp * 1000 - now; // 相差的时间戳
  if (diff <= 0) { // 秒杀时间已经过期
    clearInterval(intervalId); // 清除倒计时定时器
    document.getElementById('countdown').innerHTML = '秒杀已结束'; // 显示秒杀已结束
    return;
  }
  const remaining = { // 剩余的天、时、分、秒
    days: Math.floor(diff / (1000 * 60 * 60 * 24)),
    hours: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
    minutes: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
    seconds: Math.floor((diff % (1000 * 60)) / 1000)
  };
  // 在页面中显示剩余的天、时、分、秒
  document.getElementById('days').innerHTML = remaining.days < 10 ? "0" + remaining.days : remaining.days;
  document.getElementById('hours').innerHTML = remaining.hours < 10 ? "0" + remaining.hours : remaining.hours;
  document.getElementById('minutes').innerHTML = remaining.minutes < 10 ? "0" + remaining.minutes : remaining.minutes;
  document.getElementById('seconds').innerHTML = remaining.seconds < 10 ? "0" + remaining.seconds : remaining.seconds;
}

在计算出剩余的天、时、分、秒之后,我们需要将其显示在页面中。这里需要设置一个定时器,每秒钟调用一次countDown()函数来更新显示的剩余时间。

示例代码:

const intervalId = setInterval(countDown, 1000); // 每秒调用一次countDown()函数

示例说明

示例一:直接设置时间戳

假设我们希望在2020年8月25日00:00:00进行秒杀,如果当前时间在这之前,那么倒计时显示剩余的天、时、分、秒,如果当前时间在这之后,那么倒计时显示秒杀已结束。

我们直接在前端设置秒杀时间戳为2020年8月25日00:00:00对应的时间戳1598323200,在页面中添加上述HTML结构,并在JS代码中添加上述计算倒计时和显示的代码即可。

<div id="countdown">
  <span id="days">00</span>
  <span id="hours">00</span>
  <span id="minutes">00</span>
  <span id="seconds">00</span>
</div>
<input type="hidden" id="timestamp" value="1598323200">
const timestamp = document.getElementById('timestamp').value;

// 倒计时计算
function countDown() {
  const now = new Date().getTime();
  const diff = timestamp * 1000 - now;
  if (diff <= 0) {
    clearInterval(intervalId);
    document.getElementById('countdown').innerHTML = '秒杀已结束';
    return;
  }
  const remaining = {
    days: Math.floor(diff / (1000 * 60 * 60 * 24)),
    hours: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
    minutes: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
    seconds: Math.floor((diff % (1000 * 60)) / 1000)
  };
  document.getElementById('days').innerHTML = remaining.days < 10 ? "0" + remaining.days : remaining.days;
  document.getElementById('hours').innerHTML = remaining.hours < 10 ? "0" + remaining.hours : remaining.hours;
  document.getElementById('minutes').innerHTML = remaining.minutes < 10 ? "0" + remaining.minutes : remaining.minutes;
  document.getElementById('seconds').innerHTML = remaining.seconds < 10 ? "0" + remaining.seconds : remaining.seconds;
}

const intervalId = setInterval(countDown, 1000);

示例二:通过后端接口获取时间戳

假设我们通过后端接口获取秒杀时间戳,即在页面加载时,向后端发送一个API请求获取秒杀时间戳。我们可以使用XMLHttpRequest或者fetch来发送请求,获取到后端返回的时间戳后,设置timestamp元素的值即可。

<div id="countdown">
  <span id="days">00</span>
  <span id="hours">00</span>
  <span id="minutes">00</span>
  <span id="seconds">00</span>
</div>
<input type="hidden" id="timestamp" value="">

<script>
  // 向后端发送API请求,获取秒杀时间戳
  const xhr = new XMLHttpRequest();
  xhr.open('GET', '/api/seckill/timestamp');
  xhr.onreadystatechange = function () {
    if (xhr.readyState === XMLHttpRequest.DONE) {
      if (xhr.status === 200) {
        const timestamp = JSON.parse(xhr.responseText).timestamp;
        document.getElementById('timestamp').value = timestamp;
        const intervalId = setInterval(countDown, 1000);
      } else {
        console.error('API请求失败');
      }
    }
  };
  xhr.send();

  // 倒计时计算
  function countDown() {
    const now = new Date().getTime();
    const diff = timestamp * 1000 - now;
    if (diff <= 0) {
      clearInterval(intervalId);
      document.getElementById('countdown').innerHTML = '秒杀已结束';
      return;
    }
    const remaining = {
      days: Math.floor(diff / (1000 * 60 * 60 * 24)),
      hours: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
      minutes: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
      seconds: Math.floor((diff % (1000 * 60)) / 1000)
    };
    document.getElementById('days').innerHTML = remaining.days < 10 ? "0" + remaining.days : remaining.days;
    document.getElementById('hours').innerHTML = remaining.hours < 10 ? "0" + remaining.hours : remaining.hours;
    document.getElementById('minutes').innerHTML = remaining.minutes < 10 ? "0" + remaining.minutes : remaining.minutes;
    document.getElementById('seconds').innerHTML = remaining.seconds < 10 ? "0" + remaining.seconds : remaining.seconds;
  }
</script>

以上就是JS实现商城秒杀倒计时功能(动态设置秒杀时间)的完整攻略,希望能够帮助到大家。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JS实现商城秒杀倒计时功能(动态设置秒杀时间) - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • JavaScript之json_动力节点Java学院整理

    JavaScript之json_动力节点Java学院整理 什么是JSON JSON(JavaScript Object Notation)是一种轻量级、易于人阅读和编写的数据交换格式,其数据结构与Javascript中对象字面量相似,因此常用于与Javascript进行数据交互。 JSON的数据格式包括两种结构类型:对象和数组。对象是一个无序的“键/值”对集…

    JavaScript 2023年6月11日
    00
  • vue使用keep-alive保持滚动条位置的实现方法

    当我们在Vue应用中使用Vue-router进行路由跳转时,如果跳转到的页面存在滚动条,那么这时候就会存在一个问题,就是当我们返回到之前的路由时,滚动条会自动回到顶部,而不是保持在之前的位置。而我们可以使用keep-alive组件来保持滚动条位置。 Vue中keep-alive组件的使用 Vue中的keep-alive组件可以帮助我们在组件切换时,保留组件状…

    JavaScript 2023年6月11日
    00
  • Cookie的使用及保存中文并用Cookie实现购物车功能

    下面是关于Cookie的使用及保存中文并用Cookie实现购物车功能的完整攻略。 什么是Cookie? Cookie是在Web服务器端存储在用户计算机上的一小段文本文件,它是HTTP协议的一部分,用于告诉服务器哪些请求来自于同一用户。服务器使用Cookie来存储用户的信息,包括登录状态、用户偏好、购物车中选中的商品等等。 Cookie有一个名称、一个值和其他…

    JavaScript 2023年6月11日
    00
  • javaScript 判断字符串是否为数字的简单方法

    判断一个字符串是否为数字,可以使用多种方法,下面是两种常用的方法。 方法一:使用正则表达式 使用正则表达式可以判断一个字符串是否为数字,通过使用 test() 函数匹配字符串,检测该字符串是否符合数字格式。 if (/^[0-9]+$/.test(str)) { console.log(‘该字符串为数字’); } else { console.log(‘该字…

    JavaScript 2023年5月28日
    00
  • 12种不宜使用的Javascript语法整理

    12种不宜使用的Javascript语法整理 在Javascript编程过程中,有一些语法在代码执行过程中会出现问题,因此不建议使用。在本文中,我们将介绍12种不宜使用的Javascript语法,以及为什么应该避免使用它们。 1. with语句 with语句可以在代码块内部将一个对象提前成为一个作用域,这样我们就可以直接访问该对象的属性和方法,而不必使用对象…

    JavaScript 2023年5月18日
    00
  • vue-cli4.5.x快速搭建项目

    下面我会详细讲解一下如何使用vue-cli4.5.x快速搭建项目的完整攻略。步骤如下: 安装vue-cli 首先需要全局安装vue-cli,如果已经安装过了可以跳过这一步骤。使用以下命令在终端中进行安装: npm install -g @vue/cli 创建新项目 使用vue-cli可以快速创建一个新项目,只需要在终端中进入想要创建项目的文件夹,然后使用以下…

    JavaScript 2023年6月11日
    00
  • JavaScript深拷贝与浅拷贝实现详解

    JavaScript深拷贝与浅拷贝实现详解 什么是拷贝? 在JavaScript中,我们经常需要将对象或者数组进行复制操作,这被称为拷贝。在拷贝过程中,我们需要注意两个概念:浅拷贝和深拷贝。 什么是浅拷贝? 浅拷贝仅仅是复制了对象或数组的引用,而并没有克隆对象或数组。也就是说,对于被拷贝的对象或数组,它们的属性仍然指向原对象或数组中的属性。浅拷贝通常使用的方…

    JavaScript 2023年6月10日
    00
  • js正则表达式中的单行模式与多行模式实例分析

    下面是一个详细讲解“js正则表达式中的单行模式与多行模式实例分析”的完整攻略: 概述 正则表达式是匹配字符串的强有力的工具,它可以方便的实现各种复杂的匹配需求。而其中的单行模式与多行模式也是正则表达式中非常重要的一部分,能够帮助我们更快捷地进行字符串匹配操作。 在 JavaScript 中,我们可以使用以下方式开启单行模式和多行模式: 单行模式:使用 /s …

    JavaScript 2023年6月10日
    00
合作推广
合作推广
分享本页
返回顶部