开发者

Ajax call with MYSQL Update with a delay in the called page

开发者 https://www.devze.com 2023-03-05 13:26 出处:网络
Im currently working on a small search search script build with Jquery and AJAX. Its a live update script that dynamically loads the results in a div.

Im currently working on a small search search script build with Jquery and AJAX. Its a live update script that dynamically loads the results in a div.

This is working perfectly but i want to keep track of what answers are being shown. I've tried putting an MYSQL update statement in the results page with a sleep() to prevent updates to the database happening to soon (some questions are shown for .5 seconds, those shouldn't be updated)

Im currently using the follow code:

sleep(5);
$id = $row_rs_results['vragen_id'];
$aantal = $row_rs_results['vraag_getoond'] + 1;
mysql_select_db($database_ruimerleven, $ruimerleven);
mysql_query("UP开发者_运维技巧DATE vragen SET vraag_getoond = $aantal WHERE vragen_id = '$id'"); 
};

The problem with this is that it slows down the page to a crawl, anyone has a better solution for this? Thanks in advance!


Wouldn't it be better to have a backend process that you send the results to? That way you'd separate the update from the gui front end and it could flush the data to the database at it's leisure. The way your doing it, it would kill performance. Another option is to kick off another process that just does the database update and communicate with it.


I'm not exactly sure what you are going for, but it sounds like you are doing an instant result thing where it brings up results as you type? If that's what you are going for then I would suggest only submitting the AJAX call after the user has stopped typing for a certain amount of time. I would achieve this by putting the AJAX call in a setTimeout, and then every time the user types it resets the timeout.

Example:

var ajax_timeout;
$("#input").keydown(function() {
    clearTimeout(ajax_timeout);
    ajax_timeout = setTimeout(ajax_call, 500);
});

The php script should be able to update the database every time it gets called.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号