How to repeat function every X seconds?

Discussion in 'PHP' started by zodiakasxxx, May 19, 2009.

  1. #1
    How to repeat mysql query every X seconds. For egzample X is $seconds . But I need to do this even then page is not opened.
    I use MySQL database and I need to add 1point of smth every $seconds . I'm creating browser based game , but this is huge problem when counting resources. Any ideas?
     
    zodiakasxxx, May 19, 2009 IP
  2. NateJ

    NateJ Active Member

    Messages:
    240
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    I don't think you'd be able to do it in PHP as the piece of script would have to be run in order for it to happen.

    However, if you make a seperate script and include it in every other script therefore, that piece of scripture would run yet the page would never be opened.

    I've confused myself writing this, so it'll probably confuse you even more :eek:
     
    NateJ, May 19, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Your best off using a crontab to run your script every second. There are ways to so it strictly with php, but in your case this would be the equivalent of an intentional memory leak, since you would have no control over the script after it begins execution.

    What sounds like a better idea, and I think that NateJ was thinking the same thing, is that you use a user executed script, so that when they load a new page, it would add the amount needed based on the last page timestamp. Basically if they looked at a page 50 seconds ago, it would add 50 to their total.

    By using this method you don't continuously use resources, and you don't risk creating memory leaks.
     
    jestep, May 19, 2009 IP
  4. egoldseller

    egoldseller Guest

    Messages:
    213
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello ,

    If you have a cPanel , you can do it with Horn Jobs , or through php script by calling Linux command ...
     
    egoldseller, May 19, 2009 IP
  5. Belier

    Belier Peon

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yep, by Cpanel you can set up a cron job. But, we aware! most hosting providers in their TOS, say clearly "min cronjob's time: 15 minutes" (Like mine XD), for prevent abuse. So, read your provider TOS carrefull :)
     
    Belier, May 19, 2009 IP
  6. NateJ

    NateJ Active Member

    Messages:
    240
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    As belier said, most hosting providers have a set minimum execution time when it comes to cron jobs, so it's recommended that you use a User executed script.
     
    NateJ, May 19, 2009 IP
  7. zodiakasxxx

    zodiakasxxx Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for information. I'm using apache server without cron job.
     
    zodiakasxxx, May 19, 2009 IP
  8. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #8
    I think you can write a trigger on mysql database to do this job.
     
    alfa_375, May 19, 2009 IP