J
J
Jonh Doe2013-01-07 22:46:15
JavaScript
Jonh Doe, 2013-01-07 22:46:15

Recursion when calling method in jQuery plugin?

Based on this translation , I began to write a plugin (image slider).
In general, the point is not what the plugin does, but how.

I need to repeatedly call the same plugin method. But here I get an error. The algorithm in the method is as follows: Animate the DOM object -> auto-increment the variable in data -> delay -> call the same method.

On the second call to $(this) I have undefined. - Do not understand why.

But I have an assumption that it’s wrong to do this, I don’t have a very good algorithm. Please tell me how to write correctly. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Valery, 2013-01-08
@CodeByZen

Most likely you are calling the method like

methods.any();

What if you try like this?
methods.any.call(this);

S
Sergey, 2013-01-08
Protko @Fesor

I think you did something wrong in the first place. you should learn the JavaScript section about function context.
jsfiddle.net/kpVJv/ - out of curiosity, I sketched a simple example of a slider in the form of a jQuery plugin (the simplest, nothing interesting), but maybe you will find something interesting.

K
kotia, 2013-01-07
@kotia

Without code, it's hard to say for sure, but first of all, you shouldn't use $(this), it's better to refer to the node more explicitly. In the case of an event, for example, through e.target

N
Nikita Gusakov, 2013-01-07
@hell0w0rd

Is it not possible to refer to an object through this instead of $(this) inside the plugin, or am I confusing?

M
Mikhail Krainov, 2013-01-09
@medved13

setTimeout("$('"+data.id+"').myPlugin('next')", data.timeout);

This is not a very good solution.
1) You are passing a string, which is actually being evalized.
2) As a function to be performed, the selector engine will twitch anew each time. And although this is the simplest selector, by id, it is still time and resources.
If you really want to write like this, then:
var myPlugin = $(data.id).myPlugin
setTimeout(function(){ myPlugin('next') }, data.timeout);

developer.mozilla.org/ru/docs/DOM/window.setTimeout

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question