


Code implemented by jquery to determine whether the countdown is over_jquery
This chapter introduces a code example. This code can determine whether the countdown of the current date has ended. There is no countdown effect in this code. It only determines whether the countdown is completed, such as the discount period of shopping websites, etc. Although in actual applications, this is rarely the case. Similar code appears, but I hope it can bring some enlightenment to viewers.
The code is as follows:
function done(){ var str=$('#end').text(); var out=str.match(/\d+/g); console.log(out); var h=parseInt(out[0]),m=parseInt(out[1]),s=parseInt(out[2]); console.log(h+'#'+m+'#'+s); var calc=h*3600+m*60+s; console.log(calc); if(calc==0){ //code } else{ console.log('等待..'); } var t=setTimeout('done()',1000); } done();
The above is just a code snippet and cannot be demonstrated. Here is an introduction to its implementation process.
1. Code comments:
1.function done(){}, this function realizes the effect of judging the end of countdown.
2.var str=$('#end').text(), get the text content in the specified element, this code should count down the current time.
3.var out=str.match(/d+/g), get the array of numbers in the time and date.
4.var h=parseInt(out[0]), m=parseInt(out[1]), s=parseInt(out[2]), get hours, minutes and seconds respectively.
5.var calc=h*3600+m*60+s, convert to seconds.
6.if(calc==0){//code}, determine whether the countdown is over, and then specify the corresponding operation.
7.var t=setTimeout('done()',1000), execute the judgment function every second.
8.done(), execute this function.
The jquery countdown code is as follows:
$(function(){ var tYear = ""; //输入的年份 var tMonth = ""; //输入的月份 var tDate = ""; //输入的日期 var iRemain = ""; //开始和结束之间相差的毫秒数 var sDate = ""; //倒计的天数 var sHour = ""; //倒计时的小时 var sMin = ""; //倒计时的分钟 var sSec = ""; //倒计时的秒数 var sMsec = ""; //毫秒数 //通用工具函数,在个位数上加零,根据传的N的参数,来设前面加几个零 function setDig(num,n){ var str = ""+num; while(str.length<n){ str="0"+str } return str; } //获得相差的天,小时,分钟,秒 function getdate(){ //创建开始时间和结束时间的日期对象 var oStartDate = new Date(); var oEndDate = new Date(); //获取文本框的值 tYear = $("#tyear").val(); tMonth = $("#tmonth").val(); tDate = $("#tdate").val(); //设置结束时间 oEndDate.setFullYear(parseInt(tYear)); oEndDate.setMonth(parseInt(tMonth)-1); oEndDate.setDate(parseInt(tDate)); oEndDate.setHours(0); oEndDate.setMinutes(0); oEndDate.setSeconds(0); //求出开始和结束时间的秒数(除以1000) iRemain = (oEndDate.getTime() - oStartDate.getTime())/1000; //总的秒数除以一天的秒数,再取出整数部分,就得出有多少天。 sDate = setDig(parseInt(iRemain/(60*60*24)),3); //总的秒数除以一天的秒数,然后取其中的余数,就是把整数天扣除之后,剩下的总秒数。 iRemain %= 60*60*24; //剩下的总秒数除以一个小时的秒数,再取整数部分,就是有多少小时。 sHour = setDig(parseInt(iRemain/(60*60)),2) //剩下的总秒数除以一个小时的秒数,再取其余数,这个余数,就是扣除小时这后,剩下的总秒数。 iRemain %= 60*60; //剩下的总秒数除以一分钟的秒数,再取其整数部分,就是有多少分钟。 sMin = setDig(parseInt(iRemain/60),2) //剩下的总秒数除以一分钟的秒数,再取其余数,这个余数,就是扣除分钟之后,剩下的总秒数。 iRemain%=60; //剩下的秒数 sSec = setDig(iRemain,2); //毫秒数 sMsec = sSec*100; } //更改显示的时间 function updateShow(){ $(".showdate span").text(tYear+"-"+tMonth+"-"+tDate); $(".count span").each(function(index, element) { if(index==0){ $(this).text(sDate); }else if(index==1){ $(this).text(sHour); }else if(index == 2){ $(this).text(sMin); }else if(index == 3){ $(this).text(sSec); }else if(index == 4){ $(this).text(sMsec); } }); } //每一秒执行一次时间更新 function autoTime(){ getdate(); //如果小于零,清除调用自己,并且返回 if(iRemain<0){ clearTimeout(setT); return; } updateShow(); var setT = setTimeout(autoTime,1000); } //点击按钮开始计时 $("button").click(function(){ autoTime(); }) })
Record areas that need attention:
1. Modulo operation:
iRemain %= 60*60*24;
is to return the remainder. In this example, the remainder is the number of seconds left after taking away the integer.
2. Tool function setDig(num,n)
You can automatically add zero in front of the incoming number according to the incoming parameters

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools