Making a live clock in javascript

Add a span element and update its text content. var span = document.getElementById(‘span’); function time() { var d = new Date(); var s = d.getSeconds(); var m = d.getMinutes(); var h = d.getHours(); span.textContent = (“0” + h).substr(-2) + “:” + (“0” + m).substr(-2) + “:” + (“0″ + s).substr(-2); } setInterval(time, 1000); <span id=”span”></span> … Read more

Set Toast Appear Length

I found a solution to this by calling toast.cancel() after a certain delay that is shorter than the standard toast duration. final Toast toast = Toast.makeText(ctx, “This message will disappear in 1 second”, Toast.LENGTH_SHORT); toast.show(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { toast.cancel(); } }, 1000);

Run code for x seconds in Java?

The design of this depends on what you want doing for 15s. The two most plausible cases are “do this every X for 15s” or “wait for X to happen or 15s whichever comes sooner”, which will lead to very different code. Just waiting Thread.sleep(15000) This doesn’t iterate, but if you want to do nothing … Read more

Mysql Average on time column?

Try this: SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(`login`))) FROM Table1; Test data: CREATE TABLE `login` (duration TIME NOT NULL); INSERT INTO `login` (duration) VALUES (’00:00:20′), (’00:01:10′), (’00:20:15′), (’00:06:50′); Result: 00:07:09

How to convert time to ” time ago ” in android

I see mainly three ways: a) built-in options using SimpleDateFormat and DateUtils SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”); sdf.setTimeZone(TimeZone.getTimeZone(“GMT”)); try { long time = sdf.parse(“2016-01-24T16:00:00.000Z”).getTime(); long now = System.currentTimeMillis(); CharSequence ago = DateUtils.getRelativeTimeSpanString(time, now, DateUtils.MINUTE_IN_MILLIS); } catch (ParseException e) { e.printStackTrace(); } b) external library ocpsoft/PrettyTime (based on java.util.Date) Here you have to use SimpleDateFormat, too, … Read more

How to format timestamp in outgoing JSON

What you can do is, wrap time.Time as your own custom type, and make it implement the Marshaler interface: type Marshaler interface { MarshalJSON() ([]byte, error) } So what you’d do is something like: type JSONTime time.Time func (t JSONTime)MarshalJSON() ([]byte, error) { //do your serializing here stamp := fmt.Sprintf(“\”%s\””, time.Time(t).Format(“Mon Jan _2”)) return []byte(stamp), … Read more

Add a running countup display timer to an iOS app, like the Clock stopwatch?

If you look in the iAd sample code from Apple in the basic banner project they have a simple timer: NSTimer *_timer; _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES]; and the the method they have – (void)timerTick:(NSTimer *)timer { // Timers are not guaranteed to tick at the nominal rate specified, so this isn’t … Read more

Maximum Call Stack Size Exceeded During a setTimeout Call

Inside getNum, you’re directly invoking the getNum function, causing the stack to exhaust. Replace the function call getNum() with the function reference getNum: function getNum() // Gets triggered by page load so innerHTML works { num += 7; // Increase and assign variable document.getElementById(‘counter’).innerHTML = num; setTimeout(getNum, 4000); // <– The correct way } Link … Read more