-1

Is it possible to have a live timer on a database or site and use that timer to check if it reaches to a certain time and when it does run a method in either java, swift, and web . For example:

if (liveTimerFromDatabaseOrOnlineSite == 20seconds) {
    method1();
} else if (liveTimerFromDatabaseOrOnlineSite == 30seconds) {
    method2();
} etc....
Mohamed Mohamed
  • 185
  • 1
  • 10
  • It would probably be simpler to fetch the current count down time when the app is launched and then track the remaining time locally rather than having to repeatedly check the time on the server – Paulw11 Feb 25 '16 at 02:24
  • Where would i place this timer though? if i put it in the actual app then every app will run on different times. I want it that when ever someone opens the app where ever they may be the same method runs at the same time. – Mohamed Mohamed Feb 25 '16 at 02:26
  • You would have a timer in your app that fires every second. When the app starts you retrieve the current value from the server, say 50 seconds remaining, and save this in a variable. Every time the timer fires you subtract one from the variable and check the remaining time, calling the appropriate methods. Another user starts their app with 45 seconds to go and starts counting down from that. Their timers aren't going to trigger at the exact same millisecond but it should be close. It isn't really clear what you are trying to do – Paulw11 Feb 25 '16 at 02:30
  • No, you got it. I just wanted to avoid the placing it in a variable and make sure it check from the timer server constantly so it does execute the same method at the same time. Like lets say the timer starts at 0 and at every 30 seconds a certain method executes, so when the live timer is at 31 the methods won't execute on any phone or web, but when it does to 60 the second method executes, and so on and so forth. – Mohamed Mohamed Feb 25 '16 at 02:33
  • I'm confused would this timer be in an app or on a server running a database? It sounded in your question like the latter but now in the comments it looks like the former. – William Rosenbloom Feb 25 '16 at 02:35
  • Polling the server will be very expensive in terms of network traffic, server capacity (if you have any significant number of users) and device battery. Assuming that the local device timer frequency is reasonably accurate (which it will be) fetching the current, starting time from the server is all that is required – Paulw11 Feb 25 '16 at 02:35

1 Answers1

0

I'm not totally sure whether your question about a server side scheduled job or is asking about a timed function in an app. Here are answers for both.

For a Server-Side Scheduled Job

I think the best way to do what you want is going to be similar to cron. Basically what these "cron jobs" do is connect the system clock to a shell that will run commands at certain times or at a specific time interval. You load a file into a specific location and in the file you specify a list of Strings that will be run as arguments in a shell when the job fires, which basically allows you to run any program you want.

For Linux:

Use actual cron. You can find a good tutorial here.

For OS X:

Use .plist files similar to cron files. You can find a good tutorial here.

For Windows:

I think, but I am not sure, you can use the Windows Task Scheduler. Tutorial is here, but this pertains to Windows 7. I've personally never done this in Windows.

For a Timed Function in an App

In Swift

Swift makes it really easy to do something like this. Just use an NSTimer.

To schedule a repeating timed function call in Swift:

var myTimer = NSTimer.scheduledTimerWithTimeInterval(MY_TIME_INTERVAL, target: myObjectContainingMyMethod, selector: "myMethod", userInfo: nil, repeats: true)

and you have a scheduled timer that repeats indefinitely. You can cancel the timer but calling

myTimer.invalidate()

For Java (Android)

It is a little more complicated. See this previous question for a good explanation.

Community
  • 1
  • 1
William Rosenbloom
  • 2,506
  • 1
  • 14
  • 37