@@ -34,9 +34,10 @@ const el = document.querySelector('#machine');
34
34
35
35
const machine = new SlotMachine (el, {
36
36
active: 1 ,
37
- delay: 450 ,
38
- auto: 1500
37
+ delay: 450
39
38
});
39
+
40
+ machine .shuffle ();
40
41
```
41
42
42
43
> Lookup the sourcecode in the [ examples page] ( http://josex2r.github.io/jQuery-SlotMachine/ ) to see more examples.
@@ -71,85 +72,70 @@ const machine = new SlotMachine(element, {
71
72
});
72
73
```
73
74
74
- | Name | Type | Default | Description |
75
- | ----------------| ------------| ---------------| -------------------------------------------------------------------------------------|
76
- | ** active** | ` Number ` | ` 0 ` | The initial visible element (0 means the first one) |
77
- | ** delay** | ` Number ` | ` 200 ` | Duration (in ms) of each spin |
78
- | ** auto** | ` Boolean ` | ` false ` | Runs the carousel mode when creating the machine |
79
- | ** spins** | ` Number ` | ` 5 ` | Number of spins after stop in carousel mode |
80
- | ** randomize** | ` Function ` | ` null ` | Function (returns number) that is going to be called to set the next active element |
81
- | ** onComplete** | ` Function ` | ` null ` | Callback after each spin in carousel mode |
82
- | ** inViewport** | ` Boolean ` | ` true ` | Only spin when the machine is inside the viewport |
83
- | ** direction** | ` String ` | ` up ` | The spin direction (possible values are ` up ` and ` down ` ) |
84
- | ** transition** | ` String ` | ` ease-in-out ` | The CSS transition |
75
+ | Name | Type | Default | Description |
76
+ | ----------------| ------------| ----------------| ------------------------------------------------------------------------------------------|
77
+ | ** active** | ` Number ` | ` 0 ` | The initial visible element (0 means the first one) |
78
+ | ** delay** | ` Number ` | ` 200 ` | Duration (in ms) of each spin |
79
+ | ** randomize** | ` Function ` | ` () => number ` | Function (returns number) that returns the next active element (random value as default) |
80
+ | ** direction** | ` String ` | ` up ` | The spin direction (possible values are ` up ` and ` down ` ) |
85
81
86
82
### Properties
87
83
88
84
- ` machine.nextActive ` : Get the next active element (only while shuffling).
89
85
- ` machine.nextIndex ` : Next element index according to the current direction.
90
86
- ` machine.prevIndex ` : Prev element index according to the current direction.
91
- - ` machine.random ` : Get rando index between the machine bounds.
92
87
- ` machine.running ` : Check if the machine is running.
93
88
- ` machine.stopping ` : Check if the machine is stopping.
94
- - ` machine.visible ` : Check if the machine is visible.
95
- - ` machine.visibleTile ` : Get the current visible element in the machine viewport.
96
- - ` machine.active ` : Alias to the ` active ` setting.
97
- - ` machine.randomize ` : Alias to the ` randomize ` setting.
98
- - ` machine.direction ` : Alias to the ` direction ` setting.
99
- - ` machine.transition ` : Alias to the ` transition ` setting.
89
+ - ` machine.active ` : The current ` active ` element.
100
90
101
91
### Methods
102
92
103
- ` machine.shuffle(spins, callback) ` : Starts spining the machine.
104
- - spins (` Number ` ): Optionally set the number of spins.
105
- - callback(` Function ` ): Callback triggered when the machine stops.
93
+ ` machine.shuffle(spins: number): Promise<void> ` : Starts spining the machine.
94
+
95
+ ** Arguments** :
96
+ - spins (` Number ` ): Optionally set the number of spins until stop.
106
97
107
98
``` javascript
108
99
// Do a single spin
109
100
machine .shuffle ();
110
- // Do a single spin and then shows an alert
111
- machine .shuffle (() => alert (' Stop!' ));
112
101
// Do 5 spins before stop
113
102
machine .shuffle (5 );
114
- // Do 7 spins and then showing an alert
115
- machine .shuffle (7 , () => alert (' Stop!' ));
116
103
// "Infinite" spins
117
- machine .shuffle (9999999 ); // O_O
104
+ machine .shuffle (Infinity );
118
105
```
119
106
120
- ` machine.stop(callback) ` : Manually stops the machine.
121
- - callback(` Function ` ): Callback triggered when the machine stops.
107
+ ` machine.stop(spins: number): Promise<void> ` : Manually stops the machine.
122
108
123
- For example, start spinning the machine and stop it after pressing a button:
109
+ ** Arguments** :
110
+ - spins (` Number ` ): Set the number of spins until stop. Use ` 0 ` to inmediate stop.
124
111
125
112
``` javascript
126
- machine .shuffle (99999 );
127
- // Add the button listener
128
- myButton .addEventListener (' click' , () => {
129
- // Stop spinning
130
- machine .stop ();
131
- });
113
+ // Start spinning the machine
114
+ machine .shuffle (Infinity );
115
+ // Do 4 spins an then stop
116
+ machine .stop (4 );
132
117
```
133
118
134
- ` machine.next() ` /` machine.prev() ` : Spin to the next/previous element.
119
+ ` machine.next(): Promise<void> ` /` machine.prev(): Promise<void> ` : Spins to the ** next/previous** element.
135
120
136
121
``` javascript
137
122
// Spin to the previous element
138
123
machine .prev ();
124
+
139
125
// Spin to the next element
140
126
machine .next ();
141
127
```
142
128
143
- ` machine.run() ` : Starts the preview mode, it will spin/stop given a delay (more info in options).
144
-
145
- ``` javascript
146
- machine .run ();
147
- ```
129
+ ### Usefull recipes
148
130
149
- ` machine.run() ` : Destroys the machine. It will be useful when you want to reuse DOM.
131
+ To create an inifite carousel effect (as the previous versions ` run ` method) use a recursive function:
150
132
151
133
``` javascript
152
- machine .destroy ();
134
+ (async function run () {
135
+ await machine .shuffle (5 )
136
+ await timeout (1000 );
137
+ run ();
138
+ })();
153
139
```
154
140
155
141
## Authors
0 commit comments