JavaScript PHP Mysql: Avoid overflow on pageviews?

So I have certain profile pages that needs to have page views. I stored the view data in mysql and php (actually laravel). Basic idea is to add view by 1 every time the profile is visited.

Basic idea is to add a php code in the profile page, like

$page->view += 1; $page->save();

But what if the user hits f5 several times? It will cause the query to run a lot and I fear it will eventually slow the app.

I was thinking of making a prompt with js when user hits f5 and an ajax call to add the view. But I was wondering if there is a better solution (like, how did youtube do their views)?

Answer:1

You can count the views after a certain amount of time on the page.

To achieve this, you will need a delayed javascript function like:

setTimeout(function(){countView()},3000); (3 second delay)

Where countView() should be an ajax call to a function that runs $page->view += 1; $page->save();

And since Ajax calls are asynchronous, the execution will not delay your application flow, although a simple increment on a page-view table should not do much harm in terms of performance

Answer:2

I'm trying to inject some scripts into a page that I'm loading into PhantomJs using the phantomjs-node bridge. When I inject MochaJs into the page, I can evaluate and the window.mocha object is ...

I'm trying to inject some scripts into a page that I'm loading into PhantomJs using the phantomjs-node bridge. When I inject MochaJs into the page, I can evaluate and the window.mocha object is ...

I am facing difficulty to bind jstree with an on-expand/on-collapse event. I am currently binding the jstree with selected_node event like this : $(function () { $("#tree").jstree({ "json_data" : { ...

I am facing difficulty to bind jstree with an on-expand/on-collapse event. I am currently binding the jstree with selected_node event like this : $(function () { $("#tree").jstree({ "json_data" : { ...

  1. bind jstree
  2. binding jstree with database in asp.net mvc

Recently we opened up sentry in a way to capture JS errors. The problem we noticed is that a lot of errors came back in other languages. We threw the error messages into google translate they looked ...

Recently we opened up sentry in a way to capture JS errors. The problem we noticed is that a lot of errors came back in other languages. We threw the error messages into google translate they looked ...

This is my code: var url = f.href.value; ed.dom.setAttribs(e, { href : 'javascript:void(0)', onclick: 'doSomething(' + url + ')' }); And the doSomething method: function doSomething(url) { ...

This is my code: var url = f.href.value; ed.dom.setAttribs(e, { href : 'javascript:void(0)', onclick: 'doSomething(' + url + ')' }); And the doSomething method: function doSomething(url) { ...

  1. passing parameters function javascript
  2. passing parameter function c++
  3. passing parameter function in r
  4. parameter passing function
  5. parameter passing function in php
  6. parameter passing functions python
  7. passing function parameter pointer
  8. parameter passing function call
  9. parameter passing function call in c
  10. passing parameter to function in shell script
  11. passing parameter to function in powershell
  12. passing parameter to function react
  13. passing parameter to function in jquery
  14. passing parameter to function in sql server
  15. passing parameter to function in bash
  16. passing parameter to function in reactjs
  17. passing parameter to function in unix
  18. passing parameter to function in vba
  19. message passing parameter function
  20. passing parameter to function type