简体   繁体   English

在x分钟后做某事,然后每n分钟再做一次

[英]Do something at x minutes then again every n-th minute

I need to show an ad when a user visits a particular screen at or after 2 mins, then whenever they visit again after 6 minutes,10,14,16... 当用户在2分钟或之后访问特定屏幕时,然后在6分钟,10、14、16之后再次访问时,我需要展示广告。

I have the code below, it works fine if the user visits the screen at minutes 2,6,10,14 etc however it does not work if the user misses these minutes. 我有下面的代码,如果用户在第2、6、10、14分钟访问屏幕,则工作正常,但是如果用户错过了这些分钟,则无法正常工作。 for example if an ad was shown at minute 2 but the user didn't come back until minute 8, the ad for minute 6 is missed. 例如,如果广告在第2分钟显示,但用户直到第8分钟才回来,则第6分钟的广告会被错过。

How can I cater for this with what i have so that if the user comes back outside of these minutes, the ad for them will still be shown? 我该如何利用我现有的内容来满足此需求,以便如果用户在这些会议记录之外回来,仍然会显示针对他们的广告?

long start = adStartTime;
long now = new Date().getTime();
int minsElapsedSinceLastAd = (int) ((now - start) / 1000 / 60);
showAd = previousRunMin !=minsElapsedSinceLastAd&& minsElapsedSinceLastAd % 4 == 2;
if (showAd) {
   showAd();
   ...
   previousRunMin = minsElapsedSinceLastAd;

}

previousRunMin is there so that if the user comes back on the same minute, the ad is not shown 在那里有previousRunMin ,所以如果用户在同一分钟回来,则不会展示广告

Consider using a ScheduledThreadPoolExecutor with scheduleAtFixedRate : The call described here: ScheduledExecutorService#scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) does as you ask, scheduling an activity in a separate thread with a specified delay and period. 考虑将ScheduledThreadPoolExecutor与scheduleAtFixedRate :此处描述的调用: ScheduledExecutorService#scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)会按您的要求执行,在具有指定延迟和时间的单独线程中安排活动。 Perhaps I could be more specific to your application but the code snippet included in your question doesn't give sufficient context to understand how the activity being scheduled works. 也许我可以更具体地针对您的应用程序,但是您的问题中包含的代码片段并未提供足够的上下文来理解所计划的活动的工作方式。

I am not sure exactly what the code before your segment is... I'll assume it's in some sort of loop. 我不确定在您的细分市场之前确切的代码是什么...我将假设它处于某种循环中。

Either way, you can do the easiest way you know how. 无论哪种方式,您都可以通过最简单的方式来了解。 If and when showAd is true set a global boolean to be true. 如果showAd为true,则将global布尔值设置为true。 Then base the add showing off that new boolean. 然后根据添加内容展示新的布尔值。 Also another major issue is there is no check to see if the user actually saw the ad... 另外另一个主要问题是无法检查用户是否实际看到了广告...

It would be so much easier if there was a check to see if the ad was seen. 如果检查一下是否看到了广告,这将非常容易。 If you do not have a check then it would be pretty hard to know. 如果您没有支票,那将很难知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM