mobiscroll instance $('#scroller').mobiscroll('getInst'); returns undefined - mobiscroll

I have this function to update the time:
updateTimerView: function(hours, minutes, seconds){
var inst = $('#scroller').mobiscroll('getInst');
inst.temp = [hours, minutes, seconds];
}
But I always get the error: TypeError: inst is undefined
This same function works if I pass the inst as parameter, for example retrieving the inst with a onBeforeShow: function (dw, inst). So, just to be clear, this works:
updateTimerView: function(inst, hours, minutes, seconds){
inst.temp = [hours, minutes, seconds];
}
Also the method getInst is not listed here: but here it says that is a way to get the scroller instance.
Any idea?

Fixed with this code:
updateTimerView: function(hours, minutes, seconds){
$('#scroller').mobiscroll('setValue', [hours, minutes, seconds],
false, 0.5); }

Related

Implementing a 15 minute timeout in React issues

I'm implementing a Save feature on a form I'm creating in React. When a User clicks save on a form, I want them to type the username/password. The next time that same user tries to save, if it's been under 15 minutes, I want them to not have to sign the save username/password. If it's over 15, I want them to authenticate again.
I stumbled across this code on a different stackoverflow question about timeouts.
calculateHours(date1 , expireTime){
const dateOne = date1;
const dateTwo = new Date();
const dateOneObj = new Date(dateOne);
const dateTwoObj = new Date(dateTwo);
const hours = Math.abs(dateTwoObj - dateOneObj)/ 36e5;
const minutes = hours/ 36e5;
return expireTime > hours ? true : false
}
Code for defining the 15 minute timeout currently.
if(this.calculateHours(localStore.time , 15)){
this.wrappedClickHandler()
This is for a 15 minute timeout, but the code is really sloppy and ugly and I was wondering if anyone has a better way of implementing this?
Unless you use a library to handle time, it is not so easy to get better code: You can do some better naming, but the operation is similar:
const isExpired = (startDate, minutes) => {
startDate = startDate instanceof Date ? startDate : new Date(startDate)
const diff = new Date() - new Date(startDate)
const minutesDiff = Math.floor((diff/1000)/60);
return minutesDiff >= minutes
}
You can use setTimeout. The function will execute after a given time
setTimeout(() => {
...your necessary code for requiring user to enter username & password...
}, 15 * 60 * 1000) // since time is to be written in milliseconds
When dealing with time, I always use epochMilli.new Date().getTime() then plus 15 minutes is (15*60*1000) milliseconds to be epochExpiresAt kept somewhere(redux?) And then you use the isExpired Function to check every time user click save.
const isExpired = (epochExpiresAt) => epochExpiresAt > new Date().getTime()

How to make a command time counter

My question is: how can I make a command clock (when you execute !count and after 4 minutes you type !time and it says 4 minutes!) in discord.js
const Discord = require('discord.js');
exports.run = (client, message) => {
var af = 0;
a = setInterval(function(){
console.log("Hi");
af = af+1;
if(af == 25){
clearInterval(a);
}
console.log(af);
}, 60000);
};
exports.help = {
name: 'time',
description: 'time?',
usage: 'time'
};
I would do it like this: when you execute !count you save the server time, when you execute !time you send back the difference between those two dates.
Pseudo-code:
var date;
if (command == 'count') {
date = new Date();
message.reply("Done");
}
if (command == 'time') {
let result = require('pretty-ms')(date ? (new Date() - date) : 0);
message.reply(result);
}
I'm using the pretty-ms npm package to format milliseconds: docs & live demo.
When someone calls !count, store the current Date somewhere. new Date() - date will give you the difference between the current time and the time you stored, in milliseconds.
Please note that if the commands are in different files, as it seems by the code you posted, you'll need to store the date in a location accessible to both files: one of the solutions is to store the date as a global variable.
// by '...' I mean that you can put it wherever you want in the global object
global['...'].date = new Date():
new Date() - global['...'].date
Edit: Date class explanation
When you create a new Date, it saves the time at the moment you create it. It's like saying "!count was executed at 04:20". When you want to check how much time has passed, you need to calculate the first date minus the current date: "!count was executed at 04:20. Now it's 05:40, so the difference is 05:40 - 04:20 = 01:20: it's 1 hour and 20 minutes since you first executed !count". That translates to new Date() - past_date = time_passed.
Since dates are stored in milliseconds that difference is in milliseconds: if you want to make it more readable, you can format it by using a function as the 'pretty-ms' package, or similar ones.
The key concept is:
When !count is called, you save a new Date() to lock that point in time
When !time is called, you get the difference by doing new Date() - past_date

Converting HH:MM String into Time Object

In my Ionic AngularJS Project, I have a string called startingTime set as 08:30
I will have a ng-repeat loop which will then make a series of items with an increasing time.
For example , the first item in the list will have 08:30, second item in the list 09:00 etc ( the increment will depend on another variable so its not 30mins every loop )
The problem is I cant seem to turn the string into a Time Object and i get null displayed most of the time if i try the following
$scope.newStartTime = moment(startTime).format('HH-MM');
OR
$scope.newStartTime = var date = new Date(startTime);
So my 2 main questions are.
How do i convert a string "08:30" into a time object ?
After its a time object, how can i make a loop so the time increases by minutes after every loop ?
Thank you.
This method work for me, that transform your startingTime into a MomentObject :
var startingTime = "8:30";
var date = moment(startingTime, "HHmm");`
console.log(date);
plunker demo
For create your timer you can make something like that :
$scope.timer = moment(startingTime,"HHmm");
$scope.setInterval = setInterval(function () { $scope.myTimer(); }, 1000);
$scope.myTimer = function () {
$scope.timer = $scope.timer.add('s', 1);
};

AngularJS' $interval.cancel not working

As stated in the title,
every time I try to use $interval.cancel to cancel an existing interval, it doesn't work.
window.recordedAudio = undefined;
var isRecordingComplete = $interval(function(){
console.log(window.recordedAudio);
if (window.recordedAudio!=undefined) $timeout.cancel(isRecordingComplete);
},100);
$timeout(function(){
window.recordedAudio = "abc";
},2000);
In fact, when window.recordedAudio!= undefined (window.recordedAudio is set to "abc" after 2000ms), the $interval keeps looping and at the same time the
console.log(window.recordedAudio)
keeps being printed (+- 10 times undefined and finally abc but it keeps printing abc rather than just stop).
You are mixing $interval and $timeout.cancel.

Sometime scope variable not getting set

This seems pretty strange to me but I hope there's someone that has come across this before and point me in the right direction.
I have a variable being set in scope within an $interval and the value is sometimes "sticking" and sometimes not.
someInterval = $interval(function() {
SomeFactory.getsomething($scope.highestnumber)
.then(function(response) {
if (response.number > $scope.highestnumber)
$scope.highestnumber = response.number;
});
}, 30000);
Most of the time this works - $scope.highestnumber is set with the updated value and 30 seconds later getSomething() is called passing the updated value.
However, every now and then (and I wish I could duplicate on demand), $scope.highestnumber is set with a new value but getSomething() is called with the value it had before the update executed. I have verified it by catching the value of $scope.highestnumber after it's been set to the new value, and then checking what's being passed into getSomething() on the next cycle.
So the sequence would be...
$scope.highestnumber = 10
getsomething(10) called
returns response.number = 11
$scope.highestnumber set to 11 (verified)
getSomething(10) called 30 seconds later

Resources