JQuery, setTimeout не работающий

Опция 1:

Запись функция, подобная этому и месту это в Ваших настройках запуска:

function myex()
   execute ':w'
   execute ':!!'
endfunction

Вы могли даже отобразиться, ключевая комбинация к нему - смотрят документы.

<час>

Опция 2 (лучше):

Взгляд на документацию для переотображения нажатий клавиш - можно быть в состоянии выполнить его через простую ключевую перекарту. Следующие работы, но имеет "filename.py" hardcoded. Возможно, можно ли закопать и выяснить ли, как заменить это текущим файлом?

:map <F2> <Esc>:w<CR>:!filename.py<CR>

После отображения этого, можно просто нажать F2 в командном режиме.

IMAP, vmap, и т.д.... отображения в различных режимах. Вышеупомянутое только относится к командному режиму. Следующее должно работать в режиме вставки также:

:imap <F2> <Esc>:w<CR>:!filename.py<CR>a

Раздел 40.1 из руководства VIM очень полезен.

50
задан madhead 12 May 2016 в 05:41
поделиться

3 ответа

You've got a couple of issues here.

Firstly, you're defining your code within an anonymous function. This construct:

(function() {
  ...
)();

does two things. It defines an anonymous function and calls it. There are scope reasons to do this but I'm not sure it's what you actually want.

You're passing in a code block to setTimeout(). The problem is that update() is not within scope when executed like that. It however if you pass in a function pointer instead so this works:

(function() {
  $(document).ready(function() {update();});

  function update() { 
    $("#board").append(".");
    setTimeout(update, 1000);     }
  }
)();

because the function pointer update is within scope of that block.

But like I said, there is no need for the anonymous function so you can rewrite it like this:

$(document).ready(function() {update();});

function update() { 
  $("#board").append(".");
  setTimeout(update, 1000);     }
}

or

$(document).ready(function() {update();});

function update() { 
  $("#board").append(".");
  setTimeout('update()', 1000);     }
}

and both of these work. The second works because the update() within the code block is within scope now.

I also prefer the $(function() { ... } shortened block form and rather than calling setTimeout() within update() you can just use setInterval() instead:

$(function() {
  setInterval(update, 1000);
});

function update() {
  $("#board").append(".");
}

Hope that clears that up.

113
ответ дан 7 November 2019 в 10:35
поделиться
setInterval(function() {
    $('#board').append('.');
}, 1000);

Вы можете использовать clearInterval, если хотите остановить его в какой-то момент.

20
ответ дан 7 November 2019 в 10:35
поделиться

SetTimeout используется для того, чтобы ваш набор кода выполнялся по истечении заданного периода времени, поэтому для ваших требований лучше использовать setInterval, потому что он будет вызывать вашу функцию каждый раз в заданный промежуток времени.

8
ответ дан 7 November 2019 в 10:35
поделиться
Другие вопросы по тегам:

Похожие вопросы: