I need to make a puzzle game with timer. So it should start with start button or with dragging one of my elements to the second block (made with sortable).
I have a working time function called 'timer'. And intervalID for setting interval and clearing it. Blocks are 'piece-container' and 'puzzle-container'.
I tried this code but it doesn't work properly, it just starts with loading the page. How should I fix it?
$('.image-box').sortable({
connectWith: '#piece-container, #puzzle-container',
containment: '#puzzle-container',
//intervalID: setInterval(timer, 1000),
})
Consider the following demo.
As I suggested in my comment, you can trigger the start of your timer in the
startorstopevents of Sortable. This demo performs the start on thestopevent:I am assuming that once one list is empty (no more children elements), the timer should stop. Placing the check in
stopallows the user to drag a piece and put it back, and not trick the timer.