未加星标

Comparing Javascript & jQuery : Build a Pomodoro Timer

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Modern web development offers an incredible variety of tools, libraries, and frameworks to build web sites/applications. As of 2016, jQuery , React , Angular , and Vue are the most popular front-end libraries/frameworks in use. They can all be used to build and accomplish similar things, but in different ways. What are the advantages/disadvantages of each? How does each approach vary? What do the new tools of React, Vue, and Angular 2 offer that Angular 1, javascript, and vanilla JS didn’t?

In this 3 part tutorial series, we’re going to build a simple pomodoro timer as a means to compare vanilla JavaScript, jQuery, React, Angular 1, Angular 2 and Vue.

What is a pomodoro timer?

The pomodoro timer is an easy-to-use productivity method. Using this method, you work in 25 minute spurts with 5 minute breaks in between. Many people find it helpful for helping them focus and work on tasks during the day.


Comparing Javascript & jQuery : Build a Pomodoro Timer

Project Overview

In this tutorial, first we will create a pomodoro timer using vanilla javascript. Then, we will re-create the same pomodoro timer using jQuery.

Here is how the final version of pomodoro timer built using vanilla javascript looks like :

See the Pen Pomodoro Timer : : Javascript by Raj Gupta ( @rajdgreat007 ) on CodePen .

Here is how the final version of pomodoro timer built using jQuery looks like :

See the Pen Pomodoro Timer : : jQuery by Raj Gupta ( @rajdgreat007 ) on CodePen .

Our pomodoro timer shows minutes, seconds, and gives simple control buttons. It’s a simple timer but can give enormous productivity gains if used right.

Building the Pomodoro Timer : Javascript

Let’s first write the html code to create various elements of the pomodoro timer.

<divid="pomodoro-app"> <divid="container"> <divid="timer"> <divid="time"> <spanid="minutes">25</span> <spanid="colon">:</span> <spanid="seconds">00</span> </div> <divid="filler"></div> </div> <divid="buttons"> <buttonid="work">Work</button> <buttonid="shortBreak">Short Break</button> <buttonid="longBreak">Long Break</button> <buttonid="stop">Stop</button> </div> </div> </div>

As you can see, nothing fancy here. Just a container with the timer and control buttons. We will be using a background filler that will keep increasing as time progresses.

Let’s apply some css to the above markup to make it look nice.

#container{ border:1px solid #333; border-radius :20px; width:400px; margin:20px auto; padding:20px; text-align:center; background : #333; } #timer{ color:#f00; font-size:50px; margin:10px auto; border : 5px solid red; border-radius:50%; width:200px; height:200px; overflow:hidden; position:relative; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; cursor:default; #time{ margin-top:70px; z-index : 1; position:relative; } /* filler width is set to 200px (i.e. equal to width of the timer) so that it completely covers the timer in background. The height is initially set to 0px and it increases as timer progresses to give background fill effect. */ #filler{ background : #ddffcc; height: 0px; width: 200px; position:absolute; bottom:0; } #buttons button{ background:#4da6ff; border:none; color:#fff; cursor:pointer; padding:5px; width:90px; margin:10px auto; font-size:14px; height : 50px; border-radius : 50px; } #buttons button#shortBreak{ background : #0c0; } #buttons button#longBreak{ background : #080; } #buttons button#stop{ background : #f00; }

Now the timer looks nice, but there is no functionality associated with it yet. Let’s add javascript to make it functional.

We will be using object oriented javascript in which all of the variables and functions of the pomodoro timer will be encapsulated inside an object. Below is the basic structure of the code :

var pomodoro = { ...…………. ……………. init: function() { } …………… ……………. }; //The pomodoro object consists of variables and methods in key value pairs. window.onload = function() { pomodoro.init(); }; /* window.onload method is used to make sure that page and DOM are loaded before we call init method as it queries and manipulates DOM. */ Let’ s addvariablesand othermethodswhichareneededto makethepomodorotimerwork. var pomodoro = { started: false, /* started variable stores the state of the timer. If it is true, it means that some task (work, short break, long break) is going on with pomodoro timer. If it is false, it means that the pomodoro is not yet started or it is stopped (by clicking stop button) */ minutes: 0, /* minutes variable stores current minute which is displayed in the pomodoro. */ seconds: 0, /* seconds variable stores current second which is displayed in the pomodoro. */ fillerHeight: 0, /* fillerHeight variable stores the height of the background filler. Initially it is set to 0. As soon as a pomodoro task starts, its height starts increasing (by a value which is stored in fillerIncrement variable) and it keeps increasing till that particular task ends. On click of stop button, it is again set to 0. */ fillerIncrement: 0, /* fillerIncrement variable stores the value by which fillerHeight should increase. */ interval: null, /* interval variable stores the reference id of the currently running interval. If we need to clear the interval, we will need this variable. Although in the demo, we are not clearing the interval, it’s always a good idea to clear the interval at some point. */ minutesDom: null, /* minutesDom : It stores the reference of the minutes element of pomodoro. It’s good to store the reference so that it can be reused instead of querying the DOM again for the same element. */ secondsDom: null, /* secondsDom variable stores the reference of the seconds element of pomodoro. */ fillerDom: null, /* fillerDom variable stores the reference of the filler element of pomodoro. */ init: function() { /* I

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: jQueryJavaReactJavaScript
分页:12
转载请注明
本文标题:Comparing Javascript &amp; jQuery : Build a Pomodoro Timer
本站链接:http://www.codesec.net/view/481470.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(45)