Asked  7 Months ago    Answers:  5   Viewed   30 times

I have this counter I made but I want it to run forever, it's really simple, what am I doing wrong here?

function timer() {
  console.log("timer!")
}

window.setInterval(timer(), 1000)

 Answers

14

You used a function call instead of a function reference as the first parameter of the setInterval. Do it like this:

function timer() {
  console.log("timer!");
}

window.setInterval(timer, 1000);

Or shorter (but when the function gets bigger also less readable):

window.setInterval( function() {
  console.log("timer!");
}, 1000)
Tuesday, June 1, 2021
 
Daveel
answered 7 Months ago
47

When you have a parameterized query that looks like this:

SELECT * FROM :table WHERE :field = :value

and you substitute values for :table, :field, and :value, you get something similar to the following (actually this is an oversimplication but illustrates the point):

SELECT * FROM 'sometable' WHERE 'somefield' = 'somevalue'

because :table and :field get the same semantic treatment as :value, ie. they are treated as strings. You generally cannot parameterize table names and column names with parameterized queries. You'll have to rethink your approach a little. You might consider dynamically constructing your prepared statement string so that the table and column name parts of the query are simple concatenations, rather than binding them with PDO. But you must be very careful that you validate/sanitize the table and column names because PDO won't protect you from SQL injection at that level.

Wednesday, March 31, 2021
 
Amber
answered 9 Months ago
93

Returns a value which can be used to cancel the timer. So, it would seem unlikely that they return the same value (unless they are reusing values and one of the timers has already been cancelled)

Mozilla states it's DOM level 0, but not part of the specification. (look at the bottom of the page)

I've got an even better reference:

Nabble says:

SetTimeout and setInterval are from the original Javascript specification, pre-ECMA. That specification is not officially standardized anywhere, but it is supported by all web browsers and most implementations of the Javascript language. (Including ActionScript.)

The pre-ECMA specs are often known as the "DOM-0" APIs. Since they have never been standardized before, it makes sense for HTML5 to finally spec the non-deprecated APIs in an attempt to provide a consistent environment across browsers. Especially when recent events have proven that there are companies who like to implement the letter of the standard, but not the spirit.

Read the original spec here, or from Sun (who was an early endorser of JavaScript).

Thursday, June 3, 2021
 
RemiX
answered 7 Months ago
66

I'd favour using setTimeout() repeatedly over using setInterval() - so many things can go wrong with setInterval() and you don't know how busy the browser is and whether the interval is realistic.

Browsers don't honour your chosen timeout or interval exactly. This is mainly for security; to prevent a flood of events from messing with the browser's ability to function normally. Chrome is better about honouring timers more accurately, though it does still slow them down significantly when a tab is in the background (see this answer), for example.

If you set a new timer with setTimeout during your existing call to slideshow.action(), then you won't get events queuing up when your browser can't quite keep up, but it will still go nice and quickly when the browser is able to do so.

You will still be able to stop the timer using the timer ID, that ID will just change often.

Friday, June 18, 2021
 
EzzDev
answered 6 Months ago
83

The main problem here is that the function PLOT is a high-level plotting function, meaning that it adds objects to the plot and will modify existing plot settings. If you look at the 'NextPlot' property for axes objects, you will see that it has three settings that determine how high-level plotting functions can affect the axes object:

  • add — Use the existing axes to draw graphics objects.

  • replace — Reset all axes properties except Position to their defaults and delete all axes children before displaying graphics (equivalent to cla reset).

  • replacechildren — Remove all child objects, but do not reset axes properties (equivalent to cla).

Since 'replace' is the default setting, the handle you set for the 'ButtonDownFcn' callback gets reset to nothing when you call PLOT, thus turning off the button-click behavior. There are two ways you can avoid this:

  • Setting the 'NextPlot' property of the axes to either 'add' (to add to the existing plot objects) or 'replacechildren' (to replace the existing plot objects but keep the current axes property settings) before you make your call to PLOT.

    HRaxes = axes('Parent', Figure, 'Position', [.05 .60 .9 .35],...
                  'XLimMode', 'manual', 'ButtonDownFcn', @HR_ButtonDown,...
                  'NextPlot', 'add');
    plot(HRaxes, data.HR_X, data.HR_Y, 'b');
    
  • Using a lower-level plotting routine (such as LINE) that doesn't modify existing plot properties:

    HRaxes = axes('Parent', Figure, 'Position', [.05 .60 .9 .35],...
                  'XLimMode', 'manual', 'ButtonDownFcn', @HR_ButtonDown);
    line(data.HR_X, data.HR_Y, 'Parent', HRaxes, 'Color', 'b');
    
Wednesday, August 25, 2021
 
kosa
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share