首页 > 解决方案 > 如何在 Qualtrics 调查中隐藏下一个按钮直到一天中的某个时间

问题描述

我试图隐藏 Qualtrics 调查中的下一个按钮,直到特定的日期/时间(我的代码中的 var 阈值)。我努力了

Qualtrics.SurveyEngine.addOnload(function()
{
    var threshold = '2020-02-07T20:00:00.000Z';
    var today = new Date();
    if(threshold < today) $('NextButton').hide();
    else $('NextButton').show();

});

Qualtrics.SurveyEngine.addOnload(function() {   
    function hideEl(element) {
        element.hide();
    }   
    var nb = $('NextButton');
    var threshold = '2020-02-07 08:12';
    var today = new Date();
    var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
    var time = today.getHours() + ":" + today.getMinutes();
    var dateTime = date+' '+time;
        hideEl.defer(nb);
    if(var dateTime < threshold ) nb.hide();
    else nb.show();

    });

Qualtrics.SurveyEngine.addOnload(function() {
    function hideEl(element) {
        element.hide();
    }
    var threshold = '2020-02-07T20:00:00.000Z';
    var today = new Date();
    var nb = $('NextButton');
    hideEl.defer(nb);
 $(this.questionId).on('display', function(event) {
        if(today<threshold) nb.hide();
        else nb.show();
    });
});

但一直无法让它工作。有任何想法吗?

谢谢!!

标签: javascriptqualtrics

解决方案


如果您希望加载页面,然后将其留在原处并等待您的按钮显示,那么它将无法工作,因为您的 onload 代码只运行一次。您可以计算页面加载时显示按钮的时间有多远(以毫秒为单位),然后设置超时以在该毫秒数过去后运行显示按钮的代码。

function onLoadCb() {
  // When do you want your button to be revealed:
  const timeToShow = new Date('<whenver your date is>);
  // Convert that to milliseconds:
  const msToShow = Number(timeToShow);
  // Get the milliseconds right now:
  const now = Date.now();
  // Do math!
  const msToWait = msToShow - now;

  // Set a timeout that will wait the number of ms that we calculated:
  setTimeout(() => {
    // That amount of time has passed, show the button!
    $('.NextButton').show();
  }, msToWait);
}

推荐阅读