3

http://jsfiddle.net/U9v2H/2/

I want the numbers to increase every 100ms while holding down the mouse. What am I doing wrong?

var i = 1;
$("#plus1").mousedown(function () {
    loopthis = setinterval(repeatingfunction(), 100);
}).mouseup(function () {
    clearInterval(loopthis);
});

function repeatingfunction() {
    $("#testarea").append(i);
    i = i + 1;
}
Coffeehouse
  • 505
  • 1
  • 3
  • 15

6 Answers6

3

You're calling the function rather than assigning a reference to it when calling setInterval (you also missed the uppercase I). This:

loopthis = setinterval(repeatingfunction(), 100);

should be:

loopthis = setInterval(repeatingfunction, 100);

In your original code it's immediately executing repeatingfunction (because that's what () does), then passing the value returned by that function (nothing) to be executed when the interval is up.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
2

It´s spelled setInterval, you also invoke the repeatingfunction before setInterval is running. I dont think you want to append a new textnode but replacing the old value with the new:

var i = 1, loopthis;
$("#plus1").mousedown(function () {
    loopthis = setInterval(repeatingfunction, 100);
}).mouseup(function () {
    clearInterval(loopthis);
});

function repeatingfunction() {
    $("#testarea").text(i);
    i = i + 1;
}

Example

voigtan
  • 8,953
  • 2
  • 29
  • 30
2

big I on setInterval and repeatingfunction should not have () after it

loopthis = setInterval(repeatingfunction, 100); //notice setInterval and repeatingfunction()

DEMO

Anton
  • 32,245
  • 5
  • 44
  • 54
2

Live Demo

var i = 1;
$("#plus1").mousedown(function () {
    loopthis = setInterval(repeatingfunction, 100);
}).mouseup(function () {
    clearInterval(loopthis);
});

function repeatingfunction() {
    $("#testarea").append(i);
    i = i + 1;
}
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
  • The only thing I can see is that the numbers are being appended to `testarea`. `.text()` looks much nicer :) [see](http://jsfiddle.net/U9v2H/18/) – Ian Brindley Sep 17 '13 at 09:24
1

The scope of loopthis needs to be global since it was undefined within the mouseup event handler. setinterval needed to be changed to setInterval and passed an anonymous function. Also instead of appending the value of i change the html of the div.

Javascript

var i = 1;
var loopthis = {};
$("#plus1").mousedown(function () {
    loopthis = setInterval(function(){repeatingfunction()}, 100);
}).mouseup(function () {
    clearInterval(loopthis);
});

function repeatingfunction() {
    $("#testarea").html(i);
    i = i + 1;
}

JS Fiddle: http://jsfiddle.net/U9v2H/16/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

There are two issues with your javascript code.

1) setinterval shouold be setInterval

2) loopthis = setInterval(repeatingfunction(), 100); should be loopthis = setInterval(repeatingfunction, 100);

Note the () in repeatingfunction() are not required.

Here is the jsfiddle

iJade
  • 23,144
  • 56
  • 154
  • 243