0

I'm working on a subscription app for a coffee shop. Right now a subscriber can sign in with their phone number. After they sign in, it will direct them to a page that will show the subscriber some stats about how much money they've spent, times visited, etc.. My question is this. If there no keys have been pressed or no clicks were made on that page is there a way a can make the page redirect back to the home page? Only if there is no action on the page from the user. I feel as though this would be a Javascript thing but I'm not sure? Let me know if you need any more info? Thanks!

sign in method:

  def search
    @subscriber = Subscriber.new
  end

  def visit
    @subscriber = Subscriber.find_by_phone_number(params[:phone_number])
    if @subscriber
      @subscriber.visit ||= 0
      @subscriber.visit += 1
      @subscriber.save
      render "visit"
    end
  end
Bitwise
  • 8,021
  • 22
  • 70
  • 161
  • I need more info.Are you using google sign in or custom? What kind of login method are you using? How are you including javascript and other things in your web page?Show at least what you have right now. – bugwheels94 Aug 28 '16 at 21:30
  • Basically I made a custom sign in that will increment their visit counter and redirect them to their show page. That is the page that I want to redirect from if no action. Do you want to see that method? – Bitwise Aug 28 '16 at 21:32

1 Answers1

4

So first of all, you have to be a little more specific with what you mean with "no action". Here's a few examples:

  • The mouse cursor hasn't been moved
  • No keys have been pressed
  • No clicks were made

It can be a combination of any of these or a lot of other things as well.

Let's say "no action" would mean "Not clicked anything and didn't press a key and did not move the mouse".

In that case it would be possible to start a timeout like this:

var redirectTimeoutId = window.setTimeout(redirectToHomepage, 15000)

This calls the function redirectToHomepage after 15 seconds (15000 milliseconds).

Now you want to have that function redirectToHomepage:

function redirectToHomepage() {
  window.location.href = '/home'; // or whatever your homepage would be
}

Okay, but how do you avoid that function being called if the user clicks? Well, you just register an event handler, but in the capture phase, so it's unlikely anything on the page will cancel you out:

window.addEventListener('click keypress mousemove', function() { 
  window.clearTimeout(redirectTimeoutId)
}, true)
geekonaut
  • 5,714
  • 2
  • 28
  • 30