how to display just top 10 of the search results - search

I am writing a livesearch using Ajax+PHP+MySQL. I have some question wish you guys help me out.
How can I display just the top 10 of the result when I type in the search box? below is the js code. thanks for helps in advance.
function showUser(eleID,str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}

You should limit your search to 10 results in the PHP file when querying the database. Check this out.

You probably want to do paging also, use a combination of LIMIT and OFFSET sql statements to help you.
http://www.petefreitag.com/item/451.cfm

you should try thinking at the first step. I would require only the first 10 results from the begining, that means in MySQL. Use LIMIT 10 in your mysql query and voila, you got only 10 results. And your scripts will be faster as well.

Related

'reloadAction' in Microsoft Bot Framework 3.15 for Node.js is not passing on 'dialogArgs'

I'm following the example from the Bot Framework pages, under 'Handle User Actions' (https://learn.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-dialog-actions?view=azure-bot-service-3.0)
// Order dinner.
bot.dialog('orderDinner', [
function(session, args, next){
if(args && args.isReloaded){
// Reload action was triggered.
}
session.send("Lets order some dinner!");
builder.Prompts.choice(session, "Dinner menu:", dinnerMenu);
}
//...other waterfall steps...
])
// Once triggered, will restart the dialog.
.reloadAction('startOver', 'Ok, starting over.', {
matches: /^start over$/i,
dialogArgs: {
isReloaded: true;
}
});
and after reloading the dialog args.isReloadedis always undefined. That is, it doesn't seem that the framework is passing through what is put in dialogArgs. Any clues as to what I might be missing? I'm using v 3.15 (or rather, the people for whom I'm working are using 3.15) -- was this something that was introduced in a later version 3, that is, after 3.5? Or is something just going wrong?
Any help much appreciated!
Tried the code with the specified version and is working correctly. There is an errant ";" in your code (which is also in the docs) that should be removed and may be the culprit. Change the following line to the below.
Hope of help!
dialogArgs: {
isReloaded: true
}

My GeoLocation Does not Seem to use Cache

I see alot of people complaining about how GeoLocation uses cache.... But my problem is that mine does not seem to use cache. My Weather API needs coordinates in order to be called, and the function to call the API is triggered once my GeoLocation gets its coordinates. The results appear on an HTML page, and if I open up a bunch quickly its almost instant, but after maybe 10 seconds it stops using the cache. I set maximumAge to 5400000, 1.5 hours, and that does not seem to work. Please let me know if you have any ideas! Thanks so much in advance.
Here is my GeoLocation Code if you are interested:
navigator.geolocation.getCurrentPosition(successFunction, errorFunction, geo_options);
function successFunction(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
console.log("lat:" + lat + " long:" + long);
callWeather(); // function that contains API call and Jquery to append
//the HTML file
}
function errorFunction(position) {
console.log('Error with Position!');
}
var geo_options = {
maximumAge: 54000000, //wait an hour and a half before updating position,
//allows for quicker calls to weather api?
timeout: 10000 // wait 10 seconds before giving up on position
};
I added this line to find out where the lag really was-
console.log("called position:"+d.getSeconds()+":"+ d.getMilliseconds());
and found out it was my weather api that was holding back the HTML display:
from the log (it is in minutes/seconds/milliseconds):
content.js:232 called position: 48:36:915
content.js:329 called weather: 48:39:150
I'm using DarkSky for my weather API. I wonder how I can speed that up...

handling out of memory in chromium

I am running a web-app on a Raspberry Pi in chromium that should be running 24/7. The main issue is that it runs out of memory and displays "He's dead Jim". I am wondering if someone can help me to:
Direct me to a chromium extension that will reload/ reboot the browser if memory runs out
Supply a possible cron job to detect when memory is running out and reboot the browser if that's the case
The aim is to keep chromium running everyday without human intervention. So any additional methods/ideas would be appreciated.
Thanks in advance!
I actually found the culprit to be a few ajax request that each ran every few seconds to check if the server is still up or not(it's a long story but has to be done).
Then I found a small memory-saving solution online: to put all the ajax requests in a variable and then clear the variable after use(I also cleared unused java-script variables application-wide). Here is an example below:
function getData(){
var request = $.ajax({
url : "/someurl",
type : "HEAD",
dataType : "json",
success : function(data) {
//use your data
}
error: function(){
//doSomething
},
cache : false
});
//HERE IS THE HACK! :)
data = null;
request.onreadystatechange = null;
request.abort = null;
request = null;
}
setTimeout(function(){
getData();
}, 0.05 * 60 * 1000)
}
P.S I found the code online.

Chrome extension delay condition

I have created a chrome extension which does something after its button is clicked.
However I dont want it be abused so I need the its code to be executed after some time.
How can I surround this code with a timeout in order to achieve this?
Thank you for reading me!
chrome.tabs.getSelected(null,function(tab) {
var Mp=tab.url.substring(0,23);
if(Mp=='https://www.example.com')
{
onWindowLoad();
chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action == "getSource")
{
...Working here
}
});
}
else
{
message.innerHTML='<span style="color: #f00">This is not a valid page</span>';
}
});
function onWindowLoad()
{
var message = document.querySelector('#message');
chrome.tabs.executeScript(null, {file: "getPagesSource.js"});
}
I had to make a compromise so just after the getSelected I added the following line:
chrome.browserAction.disable(tab.Id);
It disables the action button thus it cant be clicked while the script sends the data to the server, as with this extension I grab the tab url in order to store it in my database.
After the ajax response and adding the following
if(xhr.readyState==4)
{
message.innerHTML=xhr.responseText;
chrome.browserAction.enable(tab.Id); /////<---THAT LINE
}
the button gets ready again to be clicked.
I couldnt find the way to add a specific delay in seconds, this way seems stupid but its working, as the response's delay from my server is enough for switching the 2 states of the action button.
With this, however another problem came up, which Ill write in different question.

chrome.tabs.executeScript only working with an alert before it

I believe this is related to the asynchronous nature of chrome extensions.
This section of my code:
alert(tab.title);
chrome.tabs.executeScript(tab.id, {code:"document.title = 'test'"});
Works fine, but as soon as I remove the alert, it stops working. Is there anything I can do to remove the alert but still have the js injected?
EDIT: More Code
tabs is a global array of tab objects.
chrome.tabs.onSelectionChanged.addListener(function (tabId) {
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].id == tabId) {
var tab = tabs[i];
while (i < tabs.length) {//length-1
tabs[i] = tabs[i+1];
i++;
}
tabs.pop();
alert(tab.title);//WHY IS THIS NEEDED
chrome.tabs.executeScript(tab.id, {code:"document.title = document.title.substring(1)"});
return;
}
}
});
I am very confused. Changing it the following solves the problem:
chrome.tabs.executeScript(tab.id, {code:"setTimeout('document.title = document.title.substring(1)',100)"});
However, as soon as I change the delay to 50, the script doesn't get executed again. I would prefer not to have to have to make this delay. Does anyone know whats going on?
I know this is an old question, but if anyone is looking - wrap the chrome.tabs.executeScript method in a timeout.
setTimeout(function(){chrome.tabs.executeScript(tab.id, {code:"setTimeout('document.title = document.title.substring(1)',100)"});},500);
It's certainly not ideal but it gets the job done.
Sounds like you have a race condition. My best guess would be to change the injected code to execute on onLoad.

Resources