Problems in throttle function in js - throttling

below 'throttle' code works good. But my question is that why we are using 'this' keyword in throttle function.
what is it actually?
please describe it.
thank you very much
$("document").ready(function(){
$("input").keypress(throttle(function(e){
$(".div1").html($("#ip").val());
},1000))
function throttle(fn,dly){
var timer=null;
return function(){
clearTimeout(timer);
timer=setTimeout(function(){
fn.apply(this,arguments);
},dly);
}
}
});

fn is a function. You can call the function in the standard way - fn() or you can call the call method or apply method on it.
When calling call or apply you have the option with the first argument to set the scope the function can be called in. The scope can also be set to null for no scope.

Related

Having trouble passing variable through Controller, into Eloquent Query

I'm trying to make an ajax request along with a variable in the url of the request.
The request is supposed to retrieve results for Recipes, which has a pivot table for Flavors.
The query should retrieve all Recipes with specified flavor. I am not able to pass the $flavorID variable into the query though. Getting an undefined error.
Route::get('/api/recipes/flavor/{flavorID}', function($flavorID){
return App\Recipe::whereHas('flavors', function ($query) {
$query->where('flavor_id', $flavorID);
})->paginate(20);
});
$flavorID does not exist in the scope of the function being passed to whereHas. Use the use keyword to include the variable in the scope of the function.
Route::get('/api/recipes/flavor/{flavorID}', function($flavorID){
return App\Recipe::whereHas('flavors', function ($query) use ($flavorID) {
$query->where('flavor_id', $flavorID);
})->paginate(20);
});
See https://www.php.net/manual/en/functions.anonymous.php
When you have a sub-function, you need to pass variables into scope:
// http://example.com/api/recipies/flavor/vanilla
Route::get('/api/recipes/flavor/{flavorID}', function($flavorID){
return App\Recipe::whereHas('flavors', function ($query) use($flavorID) {
$query->where('flavor_id', $flavorID);
})->paginate(20);;
});
Adding use($flavorId) allows $flavorID to be used within function($query). If you omit it, then you get an undefined error as you're experiencing.

Is there any way to pass variable in rememberForever callback?

I have following code,
\Cache::rememberForever('Roles', function() {
return RoleModel
::where('ParentRoleID' >= $CurrenctUserRoleID)
->get();
});
Issue is: I am getting Error
Undefined variable: CurrenctUserRoleID
Question: Is there any way to pass variable in callback?
You may try this (Notice the use of use keyword):
$CurrenctUserRoleID = 1; // Some id
\Cache::rememberForever('Roles', function() use($CurrenctUserRoleID) {
return RoleModel
::where('ParentRoleID' >= $CurrenctUserRoleID)
->get();
});
Check the PHP manual: Inheriting variables from the parent scope.
PHP.net - anonymous functions - Example #3
You aren't passing anything with the callback as you are not the caller of that callback. You are telling PHP to use a variable from the parent scope.
function (...) use (...) { ... }

how to use Route::input in laravel4?

I am trying to use Laravel 4 method called Route:input("users"). But I am getting following error
Call to undefined method Illuminate\Routing\Router::input()
Any idea how Route::input() works. Is there any file I need to change.
Thanks all
Route::filter('userFilter', function () {
if (Route::input('name') == 'John') {
return 'Welcome John.';
}
});
Route::get('user/{name}', array(
'before' => 'userFilter',
function ($name) {
return 'Hello, you are not John.';
}));
It looks as though Route::input was added in Laravel 4.1, make sure this is the version you are working with if you need to use this functionality.
I assume you've read the docs, but since you asked how it works, here's the example:
Accessing A Route Parameter Value
If you need to access a route parameter value outside of a route, you may use the Route::input method:
Route::filter('foo', function()
{
// Do something with Route::input('users');
});

getJSON success function out of scope

I just have started writing AJAX functions using jquery. Here i am calling myfunction(). where check.php is returning {"count":1} or {"count":0} for which myfunction is always returning retval=false . But for {"count":1} it should return true. I dont know where i am wrong. Below is the code i am using.
function myfunction(){
var retval=false;
if($('#tag').val() != ''){
var query=$( "#tag" ).val();
$.getJSON("check.php",{
q: query
},function(data){
if(data.count==0){
$('#formerrormsg').html('Error msg');
}
else{
retval=true;
}
});
}
return retval;
}
Please Help me to understand this.
That is because, $.getJSON(..) is asynchronous. The below is what jQuery doc says about the third parameter you are passing (function(data){...}):
success(data, textStatus, jqXHR): A callback function that is executed if the request succeeds.
The function which set retval to true is executed when the AJAX call succeeds; not when you call myfunction()
There are two way you could get around this:
Make your AJAX call synchronous; the function wont return until the response is received. (Not a great idea, UI might freeze)
Modify your code -- the asynchronous way. Typically, call the function (that would ideally be depending on the return value) from within the "success-function" defined as the 3rd parameter.
It is an AJAX (asynchronous) request so return retval; is returning before the AJAX success function executes, hence always returns false. You need to change your implementation to wait for that request before returning and using retval.

Creating a new function in AJAX

I saw some sample code like this in Javascript. Is this AJAX?
(function() {
/*some code*/
})();
Thanks a lot!
Is this AJAX?
No. It's what's commonly referred to as an "immediately invoked function expression", or IIFE.
(function() {
/*some code*/
})();
//^^These parentheses cause the function to be executed
The wrapping pair of parentheses cause the construct to be an expression, rather than a declaration. That's necessary because you can't have an anonymous function declaration (you'd get a syntax error).
They are commonly used to introduce a new scope (producing what you may have heard referred to as a closure).

Resources