3 minutes countdown timer which do not resets - dynamic

when user hits refresh or close the browser also i want my timer to repeat itself after 3 minutes. I want to achieve this using mysql database in which i have a table with two columns date and time from where i am updating the countdown timer. but i need timer of only 3 minutes which repeats itself and do not reset when user try to refresh or close the browser. please help me.
<div>
<h1 id="counter"></h1>
</div>
<script>
<?php
$data = strtotime($date);
$getDate = date("F d, Y", $data);
?>
var countDownDate = new Date("<?php echo "$getDate $time"; ?>").getTime();
// Update the count down every 1 second
var x = setInterval(timer, 1000);
function timer()
{
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("counter").innerHTML = minutes + " : " + seconds;
// If the count down is over, write some text
if (distance < 0)
{
clearInterval(x);
document.getElementById("counter").innerHTML="EXPIRED" ;
location.reload();
}
}
</script>

Related

Youtrack workflow via JS - Unable to compare estimation and spent time

I just want to compare 2 periods (e.g. i want to compare between 3w4d23h58m and 20h in order to track time). All i got is
I need to convert this PT2H to just 2 hours to count percentage between estimation and spent time.
https://www.jetbrains.com/help/youtrack/devportal/v1-PeriodProjectCustomField.html?q=getMinutes
Here is an excerpt from a workflow that calculates remaining time by subtracting spent time from estimation:
action: (ctx) => {
const issue = ctx.issue;
var periodestimate = issue.Estimation;
var minutesestimate = !periodestimate ? 0 : (periodestimate.getMinutes() + 60 * (periodestimate.getHours() + 8 * (periodestimate.getDays() + 5 * periodestimate.getWeeks())));
var periodspent = ctx.issue.fields.SpentTime;
var minutesspent = !periodspent ? 0 : (periodspent.getMinutes() + 60 * (periodspent.getHours() + 8 * (periodspent.getDays() + 5 * periodspent.getWeeks())));
var remain = minutesestimate - minutesspent;
ctx.issue.fields.Remaining = dateTime.toPeriod(remain + 'm');
},
I suppose that you can use it as an example to calculate the needed percentage.

how to Calculate sum of multiple durations moment-js?

I am tryng to calculate w total working hours of each user what I did is getting the duration of each day, now I want to calculate the total working hours Given this input
durations:[ '0:30:00', '0:30:00', '0:30:00', '1:00:00' ] being respectively half an hour each and the last element being one hour ,
now I want to be able to sum all of these duration to get the total working hours
Here's what I managed to do to get the duration I am open to all of your suggestions to improve my already existing code and get my wanted result which is totalHours=HH:mm:ss
let startTime = moment(book.startTime, 'hh:mm:ss');
let endTime = moment(book.endTime, 'hh:mm:ss');
let totalSec = endTime.diff(startTime, 'seconds');
var durations = moment()
.startOf('day')
.seconds(totalSec)
.format('H:mm:ss');
result.push(durations);
Convert each duration to miliseconds
Get the sum of those miliseconds
Create moment object to format to hms
const durations = [ '0:30:00', '0:30:00', '0:30:00', '1:00:00' ];
const ms = durations.map(d => moment.duration(d).asSeconds() * 1000);
const sum = ms.reduce((prev, cur) => prev + cur, 0);
const hms = moment.utc(sum).format("HH:mm:ss");
console.log('HMS: ' + hms);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone-with-data.min.js"></script>
HMS: 02:30:00

number declared in vue data can't go under 0

I've data which is also width in my project so everytime I click for example a button it will - my data number my code does perfectly but there's one thing, it can't go under 0 any soltuons?
data:{
monsterHealth:100,
},
methods:{
attack(){
var max = 10
var min = 3
var damage = Math.max(Math.floor(Math.random() * max) + 1, min)
this.monsterHealth -= damage;
}
<div class="monster">
<h1>MONSTER</h1><div class="healthbar" :style="{width:monsterHealth + '%'}"></div>
</div>
<button class="attack" #click="attack">Attack</button>

Calculate the difference between two dates in React Native

I need to calculate the difference between two dates in days
i bring today date: new Date().toJSON().slice(0, 10) = 2019-04-17
and the other date in the same form
var msDiff = new Date("June 30, 2035").getTime() - new Date().getTime(); //Future date - current date
var daysTill30June2035 = Math.floor(msDiff / (1000 * 60 * 60 * 24));
console.log(daysTill30June2035);
You can implement it yourself, but why would you? The solution to that already exists, and somebody else has taken care (and still is taking care) that it works as it should.
Use date-fns.
import differenceInDays from 'date-fns/difference_in_days';
If you really want to bash your head, you can get difference in milliseconds and then divide by number of milliseconds in a day. Sounds good to me, but I'm not 100% sure if it works properly.
const differenceInDays = (a, b) => Math.floor(
(a.getTime() - b.getTime()) / (1000 * 60 * 60 * 24)
)
If you manipulate many dates, maybe an external library like moment.js could be useful. There are multiple add-ons like the date range one.
Once installed, you need to create a range
const start = new Date(2011, 2, 5);
const end = new Date(2011, 5, 5);
const range = moment.range(start, end);
Then could get the difference by doing something like
range.diff('months'); // 3
range.diff('days'); // 92
range.diff(); // 7945200000
Hope it could be useful :)
var d1 = new Date("2019/04/17") //firstDate
var d2 = new Date("2011/02/01") //SecondDate
var diff = Math.abs(d1-d2); //in milliseconds
Use differenceInDays, parseISO from "date-fns"
import DateTimePicker from "#react-native-community/datetimepicker";
import { differenceInDays, parseISO } from "date-fns";
let day = differenceInDays(
parseISO(your end date),
parseISO(yout first Date)
);
console.log("differenceInDays",differenceInDays)

How can I use the Twitter Search API to return all tweets that match my search query, posted only within the last five seconds?

I would like to use the API to return all tweets that match my search query, but only tweets posted within the last five seconds.
With Twitter's Search API, I can use the since_id to grab all tweets from a specific ID. However, I can't really see a good way to find the tweet ID to begin from.
I'm also aware that you can use "since:" in the actual query to use a date, but you cannot enter a time.
Can someone with Twitter API experience offer me any advice? Thanks for reading and your time!
http://apiwiki.twitter.com/Search-API-Documentation
This sounds like something you can do on your end, as created_at is one of the fields returned in the result set. Just do your query, and only use the ones that are within the last 5 seconds.
<script type="text/javascript" charset="utf-8">
// JavaScript Document
$(document).ready(function(){
// start twitter API
$.getJSON('http://twitter.com/status/user_timeline/YOUR_NAME.json?count=10&callback=?', function(data){
$.each(data, function(index, item){
$('#twitter').append('<div class="tweet"><p>' + item.text.linkify() + '</p><p><strong>' + relative_time(item.created_at) + '</strong></p></div>');
});
});
function relative_time(time_value) {
var values = time_value.split(" ");
time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
var parsed_date = Date.parse(time_value);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
delta = delta + (relative_to.getTimezoneOffset() * 60);
var r = '';
if (delta < 60) {
r = 'a minute ago';
} else if(delta < 120) {
r = 'couple of minutes ago';
} else if(delta < (45*60)) {
r = (parseInt(delta / 60)).toString() + ' minutes ago';
} else if(delta < (90*60)) {
r = 'an hour ago';
} else if(delta < (24*60*60)) {
r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
} else if(delta < (48*60*60)) {
r = '1 day ago';
} else {
r = (parseInt(delta / 86400)).toString() + ' days ago';
}
return r;
}
String.prototype.linkify = function() {
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
return m.link(m);
});
};// end twitter API
}); // ***** end functions *****
</script>
<div id="twitter">
Target Div
</div>
Are you trying to poll tweets in real time? Doesn't twitter have a limit on API req/hour. I think you'd hit that pretty fast.
Why don't you just make a call to the API every 5 seconds and grab the top 1 tweet.
Twitter API results are sorted by recent by default. Please see the following quote from twitter wiki :
Parameter to Twitter search API :
result_type: Optional. Specifies what type of search results you would prefer to receive.
* Valid values include:
o mixed: In a future release this will become the default value. Include both popular and real time results in the response.
o recent: The current default value. Return only the most recent results in the response.
o popular: Return only the most popular results in the response.
* Example: http://search.twitter.com/search.atom?q=Twitter&result_type=mixed
* Example: http://search.twitter.com/search.json?q=twitterapi&result_type=popular
* Example: http://search.twitter.com/search.atom?q=justin+bieber&result_type=recent
Please correct me if I am wrong anywhere.
Thanks and Regards,
Abhay Dandekar