Pass parameters in setInterval function
You need to create an anonymous function so the actual function isn't executed right away.
setInterval( function() { funca(10,3); }, 500 );
Javascript setinterval function with arguments
Use an anonymous function
intId = setInterval(function(){waiting(argument)}, 10000);
This creates a parameterless anonymous function which calls waiting()
with arguments
Or use the optional parameters of the setInterval()
function:
intId = setInterval(waiting, 10000, argument [,...more arguments]);
Your code ( intId = setInterval(waiting(argument), 10000);
) calls waiting()
with argument
, takes the return value, tries to treat it as a function, and sets the interval for that return value. Unless waiting()
is a function which returns another function, this will fail, as you can only treat functions as functions. Numbers/strings/objects can't be typecast to a function.
Using setInterval() for a function with parameters in JavaScript
You can create another function to act as a clojure for the setCounterText function and pass that as a parameter to setInterval.
setInterval(function() { setCounterText(anotherParameter); }, 1000);
How to set Interval , clear Interval and pass an argument (timer)
You need to somehow encapsulate the ID and the stop function inside a object or function. The ID must be in local context of the logger so it can access it when it needs to stop. It also allows you to create more then just one logger without making things to complex.
const Interval = function (fn, interval) {
this.id = setInterval(fn, interval)
this.clear= function () {
clearInterval(this.id)
}
}
// Create new logger
const myLogger = new Interval(function () {
console.log('Log me')
}, 1000)
// Clear interval after 5 seconds.
setTimeout(myLogger.clear.bind(myLogger), 5000)
How to pass dynamic parameter to setInterval function?
setInterval
passes all extraneous parameters to the callback function:
let checkExist = setInterval(function (checkedValue) {
if ((document.getElementsByClassName("switch")).length > 0) {
($(".switch") as any).kendoSwitch({
checked: checkedValue
}).data("kendoSwitch");
clearInterval(checkExist);
}
}, 100, checkedValue); // Note checkedValue is passed as the final parameter
However, you can rely on closures to make this work
load(value) {
let checkExist = setInterval(function () {
if ((document.getElementsByClassName("switch")).length > 0) {
($(".switch") as any).kendoSwitch({
checked: value
}).data("kendoSwitch");
clearInterval(checkExist);
}
}, 100)
}
Passing parameters using .call() inside a setInterval function
setInterval
does not pass arguments to the callback, so remove the self
and event
arguments. You don't "lose" the event in doing so.
$.fn.contmousedown = function(mousedownCallback)
{
var interval,
self = this;
$(this).mousedown(function(event)
{
interval = setInterval(function()
{
mousedownCallback.call(self, event);
console.log('on');
}, 0);
});
$(this).mouseup(function()
{
clearInterval(interval);
});
}
Demo: http://jsfiddle.net/mattball/9veUQ
So how can I get a continuous update on the cursor position?
Use mousemove
to capture the event.
$.fn.contmousedown = function(mousedownCallback)
{
var interval,
self = this,
event;
$(this).mousemove(function(e)
{
event = e;
});
$(this).mousedown(function ()
{
interval = setInterval(function()
{
mousedownCallback.call(self, event);
}, 0);
});
$(this).mouseup(function()
{
clearInterval(interval);
});
};
Demo: http://jsfiddle.net/mattball/dVaWS/
Passing dynamic variables to function using setInterval
To see changes to values, you need references to those values, and make sure that both the code that changes values and the code that read the values use a reference to the same value.
There are several ways to do this, but it will involve an object with several object properties which will have the targetted values. But you will pass the object, not the individual values.
From the little you have provided, it looks like you have a snakeGame object. You could use that object to store some of the properties, for instance, it could have a property grid. But you could also introduce another object, for storing things you'd rather not have in your snakeGame object. This could be the case for body and/or direction.
For instance:
// Use object properties instead of separate variables:
var snake = {
body: 'xxxx',
direction: 'L'
}
var snakeGame = {
// You can also add properties to your existing object:
grid: [[0,0,0],[0,0,0],[0,0,0]],
createInterval: function(f, snake, interval) {
// Pass `this` and snake objects:
setInterval(f.bind(this, snake), interval);
},
move: function(snake) {
// use this.grid, snake.body and snake.direction:
//....
}
// etc
};
// Call with objects only (except for the constant interval)
snakeGame.createInterval(snakeGame.move, snake, interval);
// When you change direction:
$('#up').keydown(function () {
// ... do not assign to snake, but to snake.direction:
snake.direction = 'U';
// ...
// Same principle with snake.body.
});
I just chose to define grid as a property of snakeGame, and direction and body as properties of a newly introduced object. But you may want to do it differently. This is just an example. The essence is that you pass along objects, not primitive values, but that you assign to properties of those objects, never redefining the objects themselves.
Related Topics
Understanding Meteor Publish/Subscribe
How to Programmatically Open the Devtools from a Google Chrome Extension
How to Screenshot Website in JavaScript Client-Side/How Google Did It? (No Need to Access Hdd)
How to Watch for Array Changes
How to Wrap/Surround Highlighted Text with an Element
How to Find the Height of Text on an HTML Canvas
Get Functions (Methods) of a Class
Is JavaScript an Untyped Language
Jquery (Or Pure Js) Simulate Enter Key Pressed for Testing
How to Capitalize First Letter of Each Word, Like a 2-Word City
JavaScript Associative Array to JSON
Ajax Mailchimp Signup Form Integration
Merge Multiple Objects Inside the Same Array into One Object