简体   繁体   English

无法让setInterval和setTimeout正常工作

[英]Trouble getting setInterval and setTimeout to work

This uses Raphaeljs to draw a single chord from an array: 这使用Raphaeljs从数组绘制单个和弦:

function createChordStruct(key, string, shape) {
  var string = string.toUpperCase();
  var position = positions[string][key];
  var struct = chord_shapes[shape];

return {
  name: key + struct.name,
  chord: struct.chord,
  position: position,
  position_text: struct.position_text,
  bars: struct.bars
  }
}

function createChordElement(chord_struct) {
  var chordbox = $('<div>').addClass('chord');
  var chordcanvas = $('<div>');
  var chordname = $('<div>').addClass('chordname');

chordbox.append(chordcanvas);
chordbox.append(chordname);
chordname.append(chord_struct.name);


var paper = Raphael(chordcanvas[0], 150, 140);
var chord = new ChordBox(paper, 30, 30);

chord.setChord(
  chord_struct.chord,
  chord_struct.position,
  chord_struct.bars,
  chord_struct.position_text);
chord.draw();

return chordbox;


}

function createSectionElement(section_struct) {
  var section = $('<div>').addClass('section');
  var section_title = $('<div>').addClass('title');
  var section_desc = $('<div>').addClass('description');

  section.append(section_title); 
  section.append(section_desc);
  section_title.append(section_struct.section);
  section_desc.append(section_struct.description);

  return section;

} 

And this takes each chord created from the array and puts them in a new div called "chordscroller": 然后将从数组创建的每个和弦放入到名为“ chordscroller”的新div中:

function c_i() {


var randomholder = 'id_' + (Math.floor(Math.random() * 100005) + 1);
var randomId = 'id_' + (Math.floor(Math.random() * 100005) + 1);
$(function () {
  $('#sortable').append($('<li id ="' + randomholder + '" class="chordbox"><span id="i"       class="scale">C - I</span><span id="' + randomId + '" class=" even scrollpane chordscroller"></span></li>').sortable( "refresh" ))
  });


function c_one() {
  var container = $("#" + randomId + "");
  var column = null;
  var column = _.shuffle(c_1);


for (var i = 0; i < column.length; ++i) {
  var section_struct = column[i];
  var section = createSectionElement(section_struct);

  for (var j = 0; j < section_struct.chords.length; ++j)  {
    section.append(createChordElement(section_struct.chords[j]));

  }

container.append(section);
}


}



$(function() { c_one() });


}

The problem is it draws all the chords at the same time and it takes forever. 问题在于,它会同时绘制所有和弦,并且需要很长时间。 I've tried every combination of setTimeout and setInterval I could think of but I keep running into errors. 我已经尝试过setTimeout和setInterval的所有组合,但是我一直遇到错误。

Can anybody tell from this code how to get the chords to be drawn one at a time instead of all at once? 有人可以从这段代码中得知如何一次而不是一次绘制和弦吗?

Finally figured it out (using a plugin called doTimeout ): 最终弄清楚了(使用名为doTimeout的插件):

$.doTimeout( 1, function() {
  chord.setChord(
  chord_struct.chord,
  chord_struct.position,
  chord_struct.bars,
  chord_struct.position_text);
  chord.draw();


});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM